【问题标题】:PHP OpenDir function doesnt work on my Ubuntu Linux machinePHP OpenDir 函数在我的 Ubuntu Linux 机器上不起作用
【发布时间】:2017-05-18 11:46:34
【问题描述】:

最近我将机器的操作系统从 Windows 更改为 Linux,但现在我的 PHP 应用程序似乎没有在新机器中运行 opendir() 函数。以下是我的代码:

public function scandirs($dir){

    $listDir = array();

    if($handler = @opendir($dir)) {

        while (($sub = readdir($handler)) !== FALSE) {

            if ($sub != "." && $sub != ".." && $sub != "Thumb.db") {

                if(is_file($dir."/".$sub)) {

                    $listDir[] = $dir.'\\'.$sub;
                }
                elseif(is_dir($dir."/".$sub)){

                    $listDir[$sub] = scandirs($dir."\\".$sub);
                }
            }
        }   

        closedir($handler);
    }

    return $listDir;  
}

如果我执行var_dump(opendir($dir));,它只会返回一个错误。

编辑

我需要补充一点,我正在从 Linux 终端运行文件。

【问题讨论】:

  • 变量$dir的内容是什么?
  • 地址是/var/www/html/esurance365/app/sys/project
  • 如果您从命令行(终端)运行脚本,它将使用您的权限执行。终端中ls /var/www/html/esurance365/app/sys/project 的输出是什么? project 是目录吗? opendir() 如果其第一个参数不是目录(或不允许打开它)则失败。删除errors suppression operator (@),让它显示失败的原因。
  • @axiac 我已经删除了它,它只返回一个 bool(false)
  • 您的错误可能在全局级别被抑制(或登录到php_errors.log)。将error_reporting(E_ALL); ini_set('display_errors', '1'); 放在脚本顶部,您将在屏幕上看到它们。

标签: php linux file opendir


【解决方案1】:

可能是目录权限问题。检查您尝试打开的目录的权限。 文件权限和文件路径是我们将 php 应用程序从 windows 环境切换到 Linux 环境时面临的两个常见问题。

【讨论】:

  • 文件权限为全读、写和执行权限
  • 你是通过浏览器还是通过cron执行脚本?
  • 不通过 Linux 终端
  • 您登录的用户是否拥有您之前提到的所有权限?
猜你喜欢
  • 2013-09-22
  • 1970-01-01
  • 2018-05-26
  • 2021-06-10
  • 2012-01-01
  • 2010-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多