windows 如何修复 PHP 警告:file_get_contents?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7587989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:10:21  来源:igfitidea点击:

How to fix PHP Warning: file_get_contents?

phpwindowsxampp

提问by user966585

I'm getting following warning:

我收到以下警告:

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

The line 29 of main.php reads as:

main.php 的第 29 行内容如下:

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

Here is code related to $files:

这是与 $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);

As far I have understood, its the permission error. As the file can't seem to access that folder. I'm using XAMPP on Windows 7. I dont know how can i change the folder permissions on windows.

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

PS. Please notice the folder path in the warning, it has \ and also /.

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

I don't want to turn off the Warning etc., instead want to fix the warning.

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

采纳答案by James Williams

Marc B hit it on the head. You need to add a test to check if the filename is a directory with php function is_dir($filename)

马克 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 );
        }
    }

Edit

编辑

If you are doing something like a directory view online. You could go further and include the directories in a seperate array and sort them and list them out first. and then list the files. I have created something like this for a dynamic gallery.

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

回答by Arnaldof

Try using this PHP function to replace the windows path: getcwd(). You will need to concatenate the file names.

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