本文翻译自:Deleting all files from a folder using PHP?
For example I had a folder called `Temp' and I wanted to delete or flush all files from this folder using PHP. 例如,我有一个名为“ Temp”的文件夹,我想使用PHP删除或刷新此文件夹中的所有文件。 Could I do this? 我可以这样做吗?
#1楼
参考:https://stackoom.com/question/jh9G/使用PHP从文件夹中删除所有文件
#2楼
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的提示!)
#3楼
Posted a general purpose file and folder handling class for copy, move, delete, calculate size, etc., that can handle a single file or a set of folders. 发布用于复制,移动,删除,计算大小等的通用文件和文件夹处理类,该类可以处理单个文件或一组文件夹。
https://gist.github.com/4689551 https://gist.github.com/4689551
To use: 使用方法:
To copy (or move) a single file or a set of folders/files: 要复制(或移动)单个文件或一组文件夹/文件:
$files = new Files();
$results = $files->copyOrMove('source/folder/optional-file', 'target/path', 'target-file-name-for-single-file.only', 'copy');
Delete a single file or all files and folders in a path: 删除路径中的单个文件或所有文件和文件夹:
$files = new Files();
$results = $files->delete('source/folder/optional-file.name');
Calculate the size of a single file or a set of files in a set of folders: 计算单个文件或一组文件夹中的一组文件的大小:
$files = new Files();
$results = $files->calculateSize('source/folder/optional-file.name');
#4楼
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;
#5楼
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."/*"));
}
}
}
#6楼
I updated the answer of @Stichoza to remove files through subfolders. 我更新了@Stichoza的答案,以通过子文件夹删除文件。
function glob_recursive($pattern, $flags = 0) {
$fileList = glob($pattern, $flags);
foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
$subPattern = $dir.'/'.basename($pattern);
$subFileList = glob_recursive($subPattern, $flags);
$fileList = array_merge($fileList, $subFileList);
}
return $fileList;
}
function glob_recursive_unlink($pattern, $flags = 0) {
array_map('unlink', glob_recursive($pattern, $flags));
}