php - 如何修复 PHP 警告 : file_get_contents?

标签 php windows xampp

我收到以下警告:

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 上的文件夹权限。

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

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

最佳答案

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

编辑

如果您正在做一些类似在线目录查看的事情。您可以更进一步,将目录包含在单独的数组中,然后对它们进行排序并首先列出它们。然后列出文件。我为动态画廊创建了类似这样的东西。

关于php - 如何修复 PHP 警告 : file_get_contents?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7587989/

相关文章:

python - 是否有用于 Python 2.7 的 64 位二进制版本的 ujson 或 cjson

mysql - 如何配置 Intellij Idea 以连接到 XAMPP 中的 MySql 数据库?

php - 需要帮助检查用户是否在线

c++ - 如何更改 "show hidden files"设置?

c++ - 在 C++ 中获取特定的窗口名称

来自已卸载 XAMPP 的 MySQL 数据库?

php - 如何锁定 xampp 以防止文件被复制

php - 如何在 Heroku PHP 应用程序上启用基本身份验证?

php - PHP 中的模拟 [echo "symbol=LTC%2FBTC&side=BUY"| openssl dgst -sha256 -hmac "SECRETKEY"]

php - codeIgniter 将解析器结果分配给变量