看到你,梁松远的博客

位置 » 主页 » 代码分享 » php判断文件或目录是否可写

php判断文件或目录是否可写

 

 
/**
 * 判断文件或目录是否可写
 *
 * @param  string
 * @return boolean
 */
function is_really_writable($file)
{
// If we're on a Unix server with safe_mode off we call is_writable
if (DIRECTORY_SEPARATOR == '/' and @ini_get('safe_mode') == FALSE)
{
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, '/') . '/is_really_writable_' . md5(rand(1, 100));
 
if (! @file_put_contents($file, 'is_really_writable() test file'))
{
return FALSE;
}
else
{
@unlink($file);
}
 
return TRUE;
}
else if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
{
return FALSE;
}
 
return TRUE;
}