How to fix PHP Warning: file_get_contents?
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:
$other[$filename] = file_get_contents( $filename );
Here is code related to $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.
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.
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)
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.
Try using this PHP function to replace the windows path: getcwd(). You will need to concatenate the file names.
精彩评论