is_writeable函數bug問題

來源:互聯網
上載者:User
【題目】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教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.