【问题标题】:How to fix PHP Warning: file_get_contents?如何修复 PHP 警告:file_get_contents?
【发布时间】:2011-11-27 03:05:52
【问题描述】:

我收到以下警告:

Warning: file_get_contents(C:\xampp\htdocs\test/wp-content/themes/test\images) [function.file-get-contents]: failed to open stream: Permission denied in ..\plugins\theme-check\main.php on line 29

main.php 的第 29 行内容为:

$other[$filename] = file_get_contents( $filename );

这里是与 $files 相关的代码:

$files = listdir( $theme );

$files = array_merge( listdir( $parent ), $files );

if ( $files ) {
        foreach( $files as $key => $filename ) {
            if ( substr( $filename, -4 ) == '.php' ) {
                $php[$filename] = php_strip_whitespace( $filename );
            }
            else if ( substr( $filename, -4 ) == '.css' ) {
                $css[$filename] = file_get_contents( $filename );
            }
            else {
                $other[$filename] = file_get_contents( $filename );
            }
        }

        // run the checks
        $failed = !run_themechecks($php, $css, $other);

据我了解,是权限错误。由于该文件似乎无法访问该文件夹。我在 Windows 7 上使用 XAMPP。我不知道如何更改 Windows 上的文件夹权限。

PS。请注意警告中的文件夹路径,它有\和/。

我不想关闭警告等,而是想修复警告。

【问题讨论】:

  • 您是要对名为 images 的文件夹还是在名为 images 的文件夹内的文件上执行 file_get_contents 操作?
  • 您需要找到创建 $filename 的行或传递给它的内容以从中获取内容。此信息将有助于找出问题所在
  • @James Williams 我刚刚添加了代码。请看一看。
  • 如果 $filename 为空,那么您生成的 $other['filename'] 将只包含一个目录。你不能在目录上使用file_get_contents()
  • @MarcB 谢谢。詹姆斯·威廉姆斯提供了帮助。

标签: php windows xampp


【解决方案1】:

Marc B 击中了它的头部。您需要添加一个测试以检查文件名是否为带有 php 函数 is_dir($filename) 的目录

   if ( $files ) {
    foreach( $files as $key => $filename ) {
        if ( substr( $filename, -4 ) == '.php' ) {
            $php[$filename] = php_strip_whitespace( $filename );
        }
        else if ( substr( $filename, -4 ) == '.css' ) {
            $css[$filename] = file_get_contents( $filename );
        }
        else {
            if(!is_dir($filename)) $other[$filename] = file_get_contents( $filename );
        }
    }

编辑

如果您正在执行类似在线目录视图的操作。您可以更进一步,将目录包含在一个单独的数组中,然后对它们进行排序并首先列出它们。然后列出文件。我为动态图库创建了类似的东西。

【讨论】:

    【解决方案2】:

    尝试使用这个 PHP 函数来替换 windows 路径:getcwd()。 您需要连接文件名。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-09
      • 1970-01-01
      • 2017-10-18
      • 2016-09-28
      • 2023-03-18
      • 2019-08-08
      • 2020-11-28
      相关资源
      最近更新 更多