php 使用PHP从文件夹中删除所有文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4594180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Deleting all files from a folder using PHP?
提问by getaway
For example I had a folder called `Temp' and I wanted to delete or flush all files from this folder using PHP. Could I do this?
例如,我有一个名为“Temp”的文件夹,我想使用 PHP 从该文件夹中删除或刷新所有文件。我可以这样做吗?
回答by Floern
$files = glob('path/to/temp/*'); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file))
unlink($file); // delete file
}
If you want to remove 'hidden' files like .htaccess, you have to use
如果要删除 .htaccess 等“隐藏”文件,则必须使用
$files = glob('path/to/temp/{,.}*', GLOB_BRACE);
回答by Stichoza
If you want to delete everything from folder (including subfolders) use this combination of array_map
, unlink
and glob
:
如果要删除文件夹(包括子文件夹)中的所有内容,请使用array_map
,unlink
和 的组合glob
:
array_map( 'unlink', array_filter((array) glob("path/to/temp/*") ) );
This call can also handle empty directories ( thanks for the tip, @mojuba!)
此调用还可以处理空目录(感谢您的提示,@mojuba!)
回答by Yamiko
Here is a more modern approach using the Standard PHP Library (SPL).
这是使用标准 PHP 库 (SPL)的更现代的方法。
$dir = "path/to/directory";
$di = new RecursiveDirectoryIterator($dir, FilesystemIterator::SKIP_DOTS);
$ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST);
foreach ( $ri as $file ) {
$file->isDir() ? rmdir($file) : unlink($file);
}
return true;
回答by JakeParis
foreach (new DirectoryIterator('/path/to/directory') as $fileInfo) {
if(!$fileInfo->isDot()) {
unlink($fileInfo->getPathname());
}
}
回答by Poelinca Dorin
This code from http://php.net/unlink:
此代码来自http://php.net/unlink:
/**
* Delete a file or recursively delete a directory
*
* @param string $str Path to file or directory
*/
function recursiveDelete($str) {
if (is_file($str)) {
return @unlink($str);
}
elseif (is_dir($str)) {
$scan = glob(rtrim($str,'/').'/*');
foreach($scan as $index=>$path) {
recursiveDelete($path);
}
return @rmdir($str);
}
}
回答by Haim Evgi
$dir = 'your/directory/';
foreach(glob($dir.'*.*') as $v){
unlink($v);
}
回答by StampedeXV
回答by Dario Corno
Assuming you have a folder with A LOT of files reading them all and then deleting in two steps is not that performing. I believe the most performing way to delete files is to just use a system command.
假设你有一个文件夹,里面有很多文件,读取它们,然后分两步删除,效果并不好。我相信删除文件最有效的方法是使用系统命令。
For example on linux I use :
例如在 linux 上我使用:
exec('rm -f '. $absolutePathToFolder .'*');
Or this if you want recursive deletion without the need to write a recursive function
或者如果你想递归删除而不需要编写递归函数
exec('rm -f -r '. $absolutePathToFolder .'*');
the same exact commands exists for any OS supported by PHP. Keep in mind this is a PERFORMING way of deleting files. $absolutePathToFolder MUST be checked and secured before running this code and permissions must be granted.
PHP 支持的任何操作系统都存在相同的命令。请记住,这是一种删除文件的 PERFORMING 方式。在运行此代码之前必须检查并保护 $absolutePathToFolder 并且必须授予权限。
回答by JoyGuru
The simple and best way to delete all files from a folder in PHP
从 PHP 文件夹中删除所有文件的简单和最佳方法
$files = glob('my_folder/*'); //get all file names
foreach($files as $file){
if(is_file($file))
unlink($file); //delete file
}
Got this source code from here - http://www.codexworld.com/delete-all-files-from-folder-using-php/
从这里得到这个源代码 - http://www.codexworld.com/delete-all-files-from-folder-using-php/
回答by tommy
Another solution: This Class delete all files, subdirectories and files in the sub directories.
另一种解决方案:此类删除所有文件、子目录和子目录中的文件。
class Your_Class_Name {
/**
* @see http://php.net/manual/de/function.array-map.php
* @see http://www.php.net/manual/en/function.rmdir.php
* @see http://www.php.net/manual/en/function.glob.php
* @see http://php.net/manual/de/function.unlink.php
* @param string $path
*/
public function delete($path) {
if (is_dir($path)) {
array_map(function($value) {
$this->delete($value);
rmdir($value);
},glob($path . '/*', GLOB_ONLYDIR));
array_map('unlink', glob($path."/*"));
}
}
}