2012-07-25 52 views
0

我很確定我根本沒有權限。無論如何,謝謝你的回答!我將切換到自我託管,所以我知道我有權限!PHP - 製作文件夾中的文件夾

(我會刪除這一點,但它說我不能B/C有答案)

+3

也許文件夾已經存在,你想先檢查一下嗎? – Ibu 2012-07-25 19:47:17

+0

它不!我試着用差異用戶名,每次它不存在 – Primm 2012-07-25 19:47:34

+0

看起來你應該添加一個檢查周圍的mkdir如果目錄存在,或者可能抑制錯誤/警告 – ernie 2012-07-25 19:47:37

回答

0

這是爲這種情況的算法。

  1. 檢查文件夾是否存在。
  2. 如果文件夾存在,則將該文件夾命名爲其他名稱或向其添加一個隨機數。
  3. 創建新文件夾。

    http://pastebin.com/VefbrzRS

0

試試這個。

mkdir ("./files/$username"); 
0

不是那些相同的文件夾?文件/ Alex和文件/ Alex /是相同的。你的意思是文件/ $用戶名和文件/ $用戶名/文件?你正在做同樣的目錄兩次,所以這是錯誤

+0

這些是我已經嘗試過的兩種不同的東西 – Primm 2012-07-25 20:00:48

+0

轉到命令行並執行mkdir文件/測試然後在不更改目錄的情況下執行mkdir文件/ test /您將得到相同的錯誤 – 2012-07-25 20:02:32

0

如果你在Linux或MacOs上,還有另一種情況,將調用你的shell的mkdir函數。

它會看起來像:

system('mkdir -p yourdir/files/$username') 
1

首先,什麼是$username的實際價值?你是否證實它不是空的?

像這樣處理文件系統會導致幾個不同的問題。我喜歡進行很多額外的檢查,所以如果出現問題,我會更容易知道原因。我也喜歡在可能的情況下處理絕對目錄名稱,所以我不會遇到相對路徑問題。

$filesDir = '/path/to/files'; 
if (!file_exists($filesDir) || !is_dir($filesDir)) { 
    throw new Exception("Files directory $filesDir does not exist or is not a directory"); 

} else if (!is_writable($filesDir)) { 
    throw new Exception("Files directory $filesDir is not writable"); 
} 

if (empty($username)) { 
    throw new Exception("Username is empty!"); 
} 

$userDir = $filesDir . '/' . $username; 

if (file_exists($userDir)) { 
    // $userDir should be all ready to go; nothing to do. 
    // You could put in checks here to make sure it's 
    // really a directory and it's writable, though. 

} else if (!mkdir($userDir)) { 
    throw new Exception("Creating user dir $userDir failed for unknown reasons."); 
} 

mkdir()有一些非常有用的選項,用於設置權限並使文件夾層次更深。如果你還沒有,請查看PHP's mkdir page

爲了安全起見,請確保您的例外情況不會向最終用戶透露系統路徑。當您的代碼進入公共服務器時,您可能希望從錯誤消息中刪除文件夾路徑。或者配置一些東西,以便您的例外被記錄但不會顯示在網頁上。

相關問題