我收到以下警告:
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/