Created
September 24, 2017 17:06
-
-
Save cnBruceHong/a47633545891c9669478c4fff22ad3a6 to your computer and use it in GitHub Desktop.
php不可靠函数is_writable的另外一种安全实现
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ( ! function_exists('is_really_writable')) | |
{ | |
/** | |
* Tests for file writability | |
* | |
* is_writable() returns TRUE on Windows servers when you really can't write to | |
* the file, based on the read-only attribute. | |
* | |
* @link https://bugs.php.net/bug.php?id=54709 | |
* @param string | |
* @return bool | |
*/ | |
function is_really_writable($file) | |
{ | |
// If we're on a UNIX-like server, just is_writable() | |
if (DIRECTORY_SEPARATOR === '/') | |
{ | |
return is_writable($file); | |
} | |
/* For Windows servers and safe_mode "on" installations we'll actually | |
* write a file then read it. Bah... | |
*/ | |
if (is_dir($file)) | |
{ | |
$file = rtrim($file, '/').'/'.md5(mt_rand()); | |
if (($fp = @fopen($file, 'ab')) === FALSE) | |
{ | |
return FALSE; | |
} | |
fclose($fp); | |
@chmod($file, 0777); | |
@unlink($file); | |
return TRUE; | |
} | |
elseif ( ! is_file($file) OR ($fp = @fopen($file, 'ab')) === FALSE) | |
{ | |
return FALSE; | |
} | |
fclose($fp); | |
return TRUE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment