[英]PHP - Make folder in folder
我很確定我只是沒有權限。 無論如何,謝謝您的回答! 我將切換到自托管,所以我知道我已獲得許可!
(我會刪除它,但它說我無法b / c有答案)
首先, $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的mkdir頁面 。
為了安全起見,請確保您的例外情況不會泄露最終用戶的系統路徑。 當代碼進入公共服務器時,您可能希望從錯誤消息中刪除文件夾路徑。 或進行配置,以便您的異常得到記錄,但不會顯示在網頁上。
這是用於這種情況的算法。
創建新文件夾。
嘗試這個。
mkdir ("./files/$username");
這些文件夾不是嗎? files / Alex和files / Alex /相同。 您是指文件/ $用戶名和文件/ $用戶名/文件嗎? 你做兩次相同的目錄,這就是錯誤
如果您使用的是Linux或MacO,則還有另一種情況,那就是調用Shell的mkdir函數。
它看起來像:
system('mkdir -p yourdir/files/$username')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.