假设我有一个名为“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