我目前正在爲朋友的藝術家網頁構建一個非常低級別的CMS,允許她上傳,編輯和刪除圖像以及爲它們指定類別併發布新聞帖子關於節目等。rmdir()不刪除一個空文件夾 - PHP
我確定這個問題有一個非常簡單的解決方案,但我在編程方面的經驗不足讓我不知所措;所以在這裏。一個頁面,其中用戶可以刪除已上載的圖像上發生
的問題
問題。這是發生問題的代碼片段:
// Assign selection to variables in memory...
$img_id = $data["img_name"];
// First, collect the file path to the image being deleted...
$rs = mysql_query("SELECT img_path FROM img_uploads WHERE img_id = '$img_id'") or die(mysql_error());
list($img_path) = mysql_fetch_row($rs);
// Then delete that row from the DB...
mysql_query("DELETE FROM img_uploads WHERE img_id = '$img_id'") or die(mysql_error());
// Now, using the file path collected earlier, delete that file from the server.
unlink($img_path);
// Quickly make sure that the file has been deleted by checking if it exists... if it still exists return error.
if(file_exists($filename)) {
$err[] = "ERROR - There was an error deleting the file! Please try again.";
$_SESSION["errors"] = $err;
header("Location: img_del.php?doDel=failed");
exit();
}
// Scan the directory now that a file has been deleted to see if the dir is empty. If so, delete it. (No use in having empty folders!)
$file_types = array("gif","jpg","png"); // file types to scan for...
$path_parts = pathinfo($img_path); // get the directory from the file path...
$dir = $path_parts["dirname"] . "/"; // assign it to a new variable...
$handle = opendir($dir);
$scan = scandir($dir); // now, scan that directory...
$image_found = FALSE;
for($i=0; $i<count($scan); $i++) {
if ($scan[$i] != '.' && $scan[$i] != '..' && in_array(end(explode('.', $scan[$i])), $file_types)) {
$image_found = TRUE;
}
}
closedir($handle);
if(!$image_found) {
rmdir($dir);
}
我先刪除包含DB行的圖像信息,然後從服務器上刪除該文件。 這工作正常,但是,我也想檢查刪除該文件後,目錄是空的。我使用循環檢查目錄是否爲空,如果沒有找到文件,我運行mkdir()
。 出於某種原因,它一直返回一個錯誤,指出該目錄不是空
我在網上搜索與本網站的解決方案,但我還沒有找到一個。我確定它在那裏,但是我很難找到它,爲什麼我來到這裏。我該怎麼辦?
在此先感謝您提交的任何幫助!
注意 我還檢查了隱藏的文件和文件夾,但沒有運氣...
Here is a link to an image that pretty much sums up my problem in a nutshell
您信任資源管理器以告訴您何時目錄爲空? –
lol不完全,但我已檢查隱藏的文件,但這樣,但沒有運氣:/ – lemonpole
請確保您有權限在該目錄 chown和chmod :) – InspiredJW