使用PHP从文件夹中删除所有文件?

本文翻译自: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_mapunlinkglob以下组合:

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));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值