【问题标题】:Error when running my php script运行我的 php 脚本时出错
【发布时间】:2018-05-11 06:27:34
【问题描述】:

我在运行我的脚本时遇到了这些错误。

未能打开目录:第 6 行的 /Applications/MAMP/htdocs/sites-store/word/word2.php 中没有这样的文件或目录

警告:scandir(): (errno 2): 第 6 行 /Applications/MAMP/htdocs/sites-store/word/word2.php 中没有这样的文件或目录

警告:array_diff():参数 #1 不是第 6 行 /Applications/MAMP/htdocs/sites-store/word/word2.php 中的数组

警告:在第 7 行的 /Applications/MAMP/htdocs/sites-store/word/word2.php 中为 foreach() 提供的参数无效

这是我下面的代码,我不明白为什么它在下面声明时无法打开我的dir?有人可以帮我解决这个问题吗?

我的word2.php的代码

<?php
$numargs = count($argv);
if ($numargs > 1) {
    $folder = $argv[1];
    echo "Folder is: " . $folder . "\n";
    $files = array_diff(scandir($folder), array('.', '..')); //line 6
    foreach ($files as $file) { //line 7
        $filename = str_replace("í»", "", $filename);
    }
} else {
    echo "You need to pass the folder absolute path";
    exit();
}

使用此命令运行我的脚本的代码./run.bat 这是带有以下代码的文件名。

php word2.php "/Applications/MAMP/htdocs/sites-store/word/images"

PAUSE

【问题讨论】:

  • 我认为错误信息很清楚,不是吗? $folder 变量不包含有效路径。您是否进行过任何调试,例如转储该变量以查看其包含的内容?你怎么称呼这个脚本?从命令之类的?用哪些参数?你的文件夹结构是什么样的?
  • 确保请求的目录存在。 Array_diff() 确保将数组作为第一个参数。确保在 foreach 中迭代一个数组。
  • 我已经更新了我的问题,支持上面的代码看看。 @MagnusEriksson,是的,我想知道为什么来自 run.bat 的路径不起作用。

标签: php scandir array-difference


【解决方案1】:

尝试将您的 .bat 文件更改为

php word2.php -- "/Applications/MAMP/htdocs/sites-store/word/images"

PAUSE

你也应该做 var_dump($argv),看看你的脚本是如何得到它的参数的。

【讨论】:

    【解决方案2】:

    打开目录失败:中没有这样的文件或目录 /Applications/MAMP/htdocs/sites-store/word/word2.php 在第 6 行

    1. 确保您的文件在该目录中。
    2. 再次检查文件名是否有拼写错误或重复。
    3. 确保它是否已读取该目录

    【讨论】:

      猜你喜欢
      • 2018-04-12
      • 2012-04-10
      • 1970-01-01
      • 2015-01-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多