《PHP實(shí)例:php is_writable判斷文件是否可寫實(shí)例代碼》要點(diǎn):
本文介紹了PHP實(shí)例:php is_writable判斷文件是否可寫實(shí)例代碼,希望對(duì)您有用。如果有疑問,可以聯(lián)系我們。
php is_writable函數(shù)介紹PHP實(shí)例
is_writable ― 判斷給定的文件名是否可寫,該函數(shù)的結(jié)果會(huì)被緩存.請(qǐng)使用 clearstatcache() 來清除緩存.PHP實(shí)例
語法:PHP實(shí)例
bool is_writable? ( string $filename? )
PHP實(shí)例
如果文件存在并且可寫則返回 TRUE .filename 參數(shù)可以是一個(gè)允許進(jìn)行是否可寫檢查的目錄名.PHP實(shí)例
記住 PHP 也許只能以運(yùn)行 webserver 的用戶名(通常為 'nobody')來訪問文件.不計(jì)入安全模式的限制.PHP實(shí)例
參數(shù):PHP實(shí)例
filename 要檢查的文件名稱.
PHP實(shí)例
返回值:PHP實(shí)例
如果文件 filename 存在并且可寫則返回 TRUE .PHP實(shí)例
php is_writable實(shí)例PHP實(shí)例
使用is_writable函數(shù)判斷給定的文件是否可讀:PHP實(shí)例
<?php $filename = "test.text"; if (is_readable($filename)) { echo "文件 $filename 可讀"; } else { echo "文件 $filename 不可讀"; } ?>
其實(shí)我們也可以自己寫一個(gè)函數(shù)來判斷文件是否可讀,而不需要使用php內(nèi)置函數(shù)is_writable,以下函數(shù)可用于替換php內(nèi)置的is_writable函數(shù),大家可以參考一下:PHP實(shí)例
//可用于替換php內(nèi)置的is_writable函數(shù) function isWritable($filename){ if(preg_match('/\/$/',$filename)){ $tmp_file=sprintf('%s%s.tmp',$filename,uniqid(mt_rand())); return isWritable($tmp_file); } if(file_exists($filename)){ //文件已經(jīng)存在的話,使用讀寫方式打開 $fp=@fopen($filename,'r+'); if($fp){ fclose($fp); return true; } else{ return false; } } else{ $fp=@fopen($filename,'w'); if($fp){ fclose($fp); unlink($filename); return true; } else{ return false; } } }
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!PHP實(shí)例
轉(zhuǎn)載請(qǐng)注明本頁網(wǎng)址:
http://www.fzlkiss.com/jiaocheng/3073.html