【題目】PHP的is_writeable()函數存在Bug,無法準確判斷一個目錄/檔案是否可寫,請寫一個函數來判斷目錄/檔案是否絕對可寫。
【層級】六級
【解決】 下面是CodeIgniter 中的is_really_writable函數解決方案,詳見函數注釋
其中bug存在兩個方面,
1、在windowns中,當檔案只有唯讀屬性時,is_writeable()函數才返回false,當返回true時,該檔案不一定是可寫的。
如果是目錄,在目錄中建立檔案並通過開啟檔案來判斷;
如果是檔案,可以通過開啟檔案(fopen),來測試檔案是否可寫。
2、在Unix中,當php設定檔中開啟safe_mode時(safe_mode=on),is_writeable()同樣不可用。
讀取設定檔是否safe_mode是否開啟。
/** * 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. is_writable() is also unreliable * on Unix servers if safe_mode is on. * * @access private * @return void */if ( ! function_exists('is_really_writable')){ functionis_really_writable($file) {// If we're on a Unix server with safe_mode off we call is_writableif (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, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100)); if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE) { returnFALSE; } fclose($fp); @chmod($file, DIR_WRITE_MODE); @unlink($file); returnTRUE; } elseif ( ! is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE) { returnFALSE; } fclose($fp); returnTRUE; }}
').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i <= lines; i++) { $numbering.append($('
').text(i)); }; $numbering.fadeIn(1700); }); });
以上就介紹了is_writeable函數bug問題,包括了方面的內容,希望對PHP教程有興趣的朋友有所協助。