当前位置:网站首页>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/'));
边栏推荐
- Usage of map and foreach in JS
- Apt command reports certificate error certificate verification failed: the certificate is not trusted
- 查询GPU时无进程运行,但是显存却被占用了
- Eggjs -typeorm treeenity practice
- A preliminary study on ant group G6
- Win电脑截图黑屏解决办法
- JS delete the last bit of the string
- The table component specifies the concatenation parallel method
- PIP install
- Latex warning: citation "*****" on page y undefined on input line*
猜你喜欢
Solution to the black screen of win computer screenshot
CTF web practice competition
Uniapp introduces local fonts
sqli-labs通关汇总-page1
apt命令报证书错误 Certificate verification failed: The certificate is NOT trusted
【文献阅读与想法笔记13】 Unprocessing Images for Learned Raw Denoising
Queue (linear structure)
SQLI-LABS通关(less6-less14)
SQLI-LABS通关(less15-less17)
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)
随机推荐
SQLI-LABS通关(less18-less20)
SQLI-LABS通关(less1)
默认google浏览器打不开链接(点击超链接没有反应)
JS countdown case
js删除字符串的最后一个字符
Kali latest update Guide
Use of interrupt()
Tool grass welfare post
Explanation and application of annotation and reflection
查询GPU时无进程运行,但是显存却被占用了
js数组的常用的原型方法
Uniapp introduces local fonts
js创建一个自定义json数组
蚂蚁集团g6初探
Code execution sequence with and without resolve in promise
搭建frp进行内网穿透
Latex参考文献引用失败 报错 LaTeX Warning: Citation “*****” on page y undefined on input line *
pm2简单使用和守护进程
Win10:添加或者删除开机启动项,在开机启动项中添加在用户自定义的启动文件
CVE-2015-1635(MS15-034 )远程代码执行漏洞复现