2008-09-20 109 views

回答

80

是的,它的文件夾的工作....如果文件存在並且可寫

返回TRUE。 filename參數可以是一個目錄名稱,允許您檢查目錄是否可寫。

+22

訣竅是你不能在你真正想測試的文件夾中指定一個尚不存在的*文件*只需指定文件夾即可。 – philfreo 2010-09-24 17:33:51

1

stat()

很像系統統計,但在PHP中。你想要檢查的是模式值,就像你在其他語言中調用統計的方式一樣(I.E.C/C++)。

http://us2.php.net/stat

3

您可以發送一個完整的文件路徑is_writable()功能。如果該文件尚不存在於目錄中,則is_writable()將返回false。如果是這種情況,您需要檢查目錄本身,並刪除文件名。如果你這樣做,is_writable會正確地告訴你該目錄是否可寫。如果$file包含您的文件路徑做到這一點:

$file_directory = dirname($file); 

然後使用is_writable($file_directory),以確定是否該文件夾是可寫的。

我希望這可以幫助別人。

12

這是代碼:)

<?php 

$newFileName = '/var/www/your/file.txt'; 

if (! is_writable(dirname($newFileName))) { 

    echo dirname($newFileName) . ' must writable!!!'; 
} else { 

    // blah blah blah 
} 
+0

啊,`dirname($ new_file_name)`,這比我打算做的更簡單。我打算使用`pathinfo($ new_file_name,PATHINFO_DIRNAME)`。謝謝。 – 2014-04-24 07:57:23

5

成爲所有者/組/世界

$dir_writable = substr(sprintf('%o', fileperms($folder)), -4) == "0774" ? "true" : "false"; 

和平更具體的...

+0

這個功能應該改進。現在它將返回false,例如`0777`的權限。 – 2016-01-27 13:27:58

0

這是我要做的事:

file_put_contents()創建一個文件並檢查返回值,如果是肯定的(寫入的數量字節),那麼你可以繼續做你必須做什麼,如果是假的,然後它不可寫

$is_writable = file_put_contents('directory/dummy.txt', "hello"); 

if ($is_writable > 0) echo "yes directory it is writable"; 

else echo "NO directory it is not writable"; 

,那麼你可以使用的unlink()

unlink('directory/dummy.txt'); 
+0

雖然這在技術上有效,但絕對不是推薦的方法。文件系統操作相對較慢,會產生大量開銷,所以創建一個虛擬文件然後刪除/取消鏈接它比其他大多數方法慢得多。 – Byson 2015-07-29 11:27:23

0

刪除虛擬文件我寫了一個小腳本(我把它叫做isWritable。php),它檢測腳本所在的同一目錄中的所有目錄,並寫入該頁面以確定每個目錄是否可寫。希望這可以幫助。

<?php 
// isWritable.php detects all directories in the same directory the script is in 
// and writes to the page whether each directory is writable or not. 

$dirs = array_filter(glob('*'), 'is_dir'); 

foreach ($dirs as $dir) { 
    if (is_writable($dir)) { 
     echo $dir.' is writable.<br>'; 
    } else { 
     echo $dir.' is not writable. Permissions may have to be adjusted.<br>'; 
    } 
} 
?> 
相關問題