I’ve got the following function which i’m using to check if a directory is writable or not.
/**
* check if the path is writable. if the path is a folder it creates a test file.
*
* @param string $path
* @return boolean
*/
public static function is_writable( $path ) {
//will work in despite of Windows ACLs bug
//NOTE: use a trailing slash for folders!!!
//see http://bugs.php.net/bug.php?id=27609
//see http://bugs.php.net/bug.php?id=30931
if ( $path{strlen($path)-1} === DIRECTORY_SEPARATOR ) {// recursively return a temporary file path
return self::is_writable( $path . uniqid( mt_rand() ) . '.tmp' );
} else if ( is_dir( $path ) ) {
return self::is_writable( $path . DIRECTORY_SEPARATOR . uniqid( mt_rand() ) . '.tmp' );
}
$file_already_exists = file_exists( $path );
// check tmp file for read/write capabilities
$f = @fopen( $path, 'a');
if ( $f === false ) {
return false;
}
if ( ! $file_already_exists ) {
unlink( $path );
}
return true;
}
This has always worked fine until recently i always get a warning as unlink() has no permission to delete the file. But the temp file is created normally so i can write to the dir.
Warning: unlink(C:\Program Files (x86)\Zend\Apache2\htdocs\wordpress\wp-content\plugins\all-in-one-event-calendar-premium\cache\152006398050813468bb6ec.tmp) [function.unlink]: Permission denied in C:\Program Files (x86)\Zend\Apache2\htdocs\wordpress\wp-content\plugins\all-in-one-event-calendar-premium\lib\utility\class-ai1ec-filesystem-utility.php on line 35
How is this possible?I’ve tried to give 777 to the directory i’m testing and i still get the warning! I’m on Windows 7 with Zend server
Try adding an
fclose()call, you left the file pointer open after creating the file.Also consider using
touch()if all you want to do is determine whether you are able to create the file.