当前位置:网站首页>php中删除指定文件夹下的内容
php中删除指定文件夹下的内容
2022-07-02 06:23:00 【夜空の雪風】
前言
在之前做远程文件下载到本地然后打包下载的功能中存在文件重复的问题,导致下载的压缩中有之前下载过的文件,下边介绍一种方法可以在下载之前删除指定文件夹中的文件,保证每次都是最新的文件。
解决方法
/** * 删除指定文件夹下的内容 * @param $dirname * @param boolean $self * @return void */
function do_rmdir($dirname, $self = false)
{
//检查文件或目录是否存在
if (!file_exists($dirname)) {
return false;
}
//是文件进行删除
if (is_file($dirname) || is_link($dirname)) {
return unlink($dirname);
}
//开始读取目录
$dir = dir($dirname);
if ($dir) {
while (false !== $entry = $dir->read()) {
if ($entry == '.' || $entry == '..') {
continue;
}
//进行文件删除
do_rmdir($dirname . '/' . $entry);
}
}
//关闭目录
$dir->close();
//是否删除本身文件夹
$self && rmdir($dirname);
//成功返回
return true;
}
使用方法为直接调用,传入需要删除的文件夹路径。
//清空文件夹中的内容
do_rmdir(storage_path('app/public/document/remoteFiles/'));
边栏推荐
- PIP install
- js中map和forEach的用法
- Atcoder beginer contest 253 F - operations on a matrix / / tree array
- Deployment API_ automation_ Problems encountered during test
- js数组的常用的原型方法
- Cve - 2015 - 1635 (ms15 - 034) réplication de la vulnérabilité d'exécution de code à distance
- Go common compilation fails
- Selenium+msedgedriver+edge browser installation driver pit
- Sqli-labs customs clearance (less18-less20)
- A preliminary study on ant group G6
猜你喜欢

sqli-labs通关汇总-page1

Cve-2015-1635 (ms15-034) Remote Code Execution Vulnerability recurrence

Win10桌面图标没有办法拖动(可以选中可以打开可以删除新建等操作但是不能拖动)

js中对于返回Promise对象的语句如何try catch

Win电脑截图黑屏解决办法

查询GPU时无进程运行,但是显存却被占用了

There is no way to drag the win10 desktop icon (you can select it, open it, delete it, create it, etc., but you can't drag it)

QQ email cannot receive the email sent by Jenkins using email extension after construction (timestamp or auth...)

由于不正常断电导致的unexpected inconsistency;RUN fsck MANUALLY问题已解决

Usage of map and foreach in JS
随机推荐
Apt command reports certificate error certificate verification failed: the certificate is not trusted
Stress test modification solution
搭建frp进行内网穿透
SQL注入闭合判断
Use of interrupt()
Win10网络图标消失,网络图标变成灰色,打开网络设置闪退等问题解决
Brief analysis of PHP session principle
sqli-labs通关汇总-page2
Latex warning: citation "*****" on page y undefined on input line*
js删除字符串的最后一位
PXC high availability cluster summary
Sqli labs customs clearance summary-page3
[literature reading and thought notes 13] unprocessing images for learned raw denoising
Log - 7 - record a major error in missing documents (A4 paper)
Uniapp introduces local fonts
ModuleNotFoundError: No module named ‘jieba. analyse‘; ‘ jieba‘ is not a package
After reading useful blogs
Sqli-labs customs clearance (less6-less14)
由于不正常断电导致的unexpected inconsistency;RUN fsck MANUALLY问题已解决
Atcoder beginer contest 253 F - operations on a matrix / / tree array