假設我有一個名爲「temp」的目錄,它有很多自動生成的垃圾文件。如何使用PHP自動清除/清空每週一次的「臨時」目錄?我不知道那裏有什麼文件。我只想清空目錄。PHP空目錄/文件夾
1
A
回答
1
使用cron調用將刪除整個文件夾的腳本,然後再次mkdir。
2
首先你需要cron job
和php script
來擦除文件。 這裏的PHP腳本:
$fileToDelete = glob('path/to/temp/*');
foreach($fileToDelete as $file){
if(is_file($file))
unlink($file);
}
之後,你需要配置的cron每天執行這個文件,例如或者你想:
0 0 * * 0 /path/script.php //will execute every week
2
您可以使用一個代碼片段像這樣,如果您的應用程序位於基於Linux的系統上,然後運行cron作業。
$files = glob('path/to/folder/*'); // get all file names present in folder
foreach($files as $file){ // iterate files
if(is_file($file))
unlink($file); // delete the file
}
1
給你的文件夾的路徑在$ PATH
$path = $_SERVER['DOCUMENT_ROOT'].'/work/removegolder/'; // path of your directory
header('Content-type: text/plain'); // plain text for easy display
// preconditon: $dir ends with a forward slash (/) and is a valid directory
// postcondition: $dir and all it's sub-directories are recursively
// searched through for .svn directories. If a .svn directory is found,
// it is deleted to remove any security holes.
function removeSVN($dir) {
//echo "Searching: $dir\n\t";
$flag = false; // haven't found .svn directory
$svn = $dir . 'foldername';
if(is_dir($svn)) {
if(!chmod($svn, 0777))
echo "File permissions could not be changed (this may or may not be a problem--check the statement below).\n\t"; // if the permissions were already 777, this is not a problem
delTree($svn); // remove the directory with a helper function
if(is_dir($svn)) // deleting failed
echo "Failed to delete $svn due to file permissions.";
else
echo "Successfully deleted $svn from the file system.";
$flag = true; // found directory
}
if(!$flag) // no .svn directory
echo 'No directory found.';
echo "\n\n";
$handle = opendir($dir);
while(false !== ($file = readdir($handle))) {
if($file == '.' || $file == '..') // don't get lost by recursively going through the current or top directory
continue;
if(is_dir($dir . $file))
removeSVN($dir . $file . '/'); // apply the SVN removal for sub directories
}
}
// precondition: $dir is a valid directory
// postcondition: $dir and all it's contents are removed
// simple function found at http://www.php.net/manual/en/function.rmdir.php#93836
function delTree($dir) {
$files = glob($dir . '*', GLOB_MARK); // find all files in the directory
foreach($files as $file) {
if(substr($file, -1) == '/')
delTree($file); // recursively apply this to sub directories
else
unlink($file);
}
if (is_dir($dir)){
//echo $dir;
// die;
//rmdir($dir); // remove the directory itself (rmdir only removes a directory once it is empty)
}
}
// remove all directories in the
// current directory and sub directories
// (recursively applied)
removeSVN($path);
相關問題
- 1. 刪除目錄中的空文件夾
- 2. 刪除空目錄或文件夾/子文件夾
- 3. PHP顯示目錄文件夾
- 4. WAMP/PHP顯示目錄/子文件夾
- 5. 使用php的點目錄文件夾
- 6. 使用php打開目錄文件夾
- 7. PHP目錄樹 - 刪除文件夾
- 8. PHP化妝目錄文件夾
- 9. 從根目錄中的文件夾獲取目錄PHP
- 10. 文件夾子目錄「目錄」清單
- 11. PHP - 掃描目錄中的文件夾和文本文件
- 12. 文件名,目錄,文本,python文件夾子文件夾
- 13. PHP - 在localhost目錄文件夾和文件
- 14. 更改文件夾的php文件根目錄
- 15. php - 在一個目錄中列出文件夾和文件
- 16. 隱藏文件夾目錄中的PHP文件
- 17. 目錄文件夾/文件列表數組php
- 18. PHP文件目錄 - $ _ SERVER [「DOCUMENT_ROOT」]的父文件夾
- 19. 如何瀏覽文件夾或文件或目錄使用php
- 20. 保存文件在目錄文件夾
- 21. 目錄中的空文件
- 22. 使用php列出目錄中的所有文件夾子文件夾和文件請使用php
- 23. PHP文件上傳,目標目錄上方upload.php文件目錄
- 24. 遞歸文件夾/目錄複製與AS3 /空氣
- 25. htaccess從空文件夾重定向到另一個目錄
- 26. Artefact文件夾結構不包含空目錄
- 27. Shell腳本輸入目錄(文件夾名稱包含空格)
- 28. 將文件夾從iPhone Resources目錄複製到文檔目錄
- 29. 文件夾目錄中的子文件夾數量
- 30. 獲取目錄中的所有文件夾和子文件夾
@MatteoTassinari謝謝! – Paranoid