Home > Article > Backend Development > How to delete multiple files in php
php method to implement multi-file deletion: first create a PHP sample file; then obtain the file path; then perform file deletion; and finally delete the folder.
The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer.
1. First, the path must be clear
It is recommended to use an absolute path (window: D:/www/upload/aaa.jpg unix: /home/dir/xxx/sdfsdf.jpg)
The absolute path can be obtained through the __FILE__ constant, and you have to handle the specific details yourself.
If it is a relative path, it is more troublesome, because the path is based on the location of the file where the deletion code is executed.
2. If the path is set, you can delete the file. Delete the file first. When deleting the folder
$filearray = array(....文件名 数组...); $path = '绝对路径'; //这里@ 可以屏蔽 实际文件不存在时出现的报错 foreach($filearray as $v){ @unlink($path.$v['path'].$v['imgname']); @unlink($path.$v['path'].$v['thumb']); } //删除文件夹 这里其实跟上面数组就没关系了, 你应该是要删除uploadtuku里面的 所有空文件夹 //PHP5 有一个函数叫 scandir 扫描目录里面文件和文件夹 //如果是 LINUX服务器 删除目录 还需要相应的权限,一般可以上传文件的话 权限是已经给了 $files = scandir($path.'uploadtuku'); foreach($files as $v){ if($v!=='.' && $v!=='..' && is_dir($path.'uploadtuku/'.$v)){ @rmdir($path.'uploadtuku/'.$v); } }
If there are files in it, rmdir will not delete the folder.
【Recommended: PHP Video Tutorial】
The above is the detailed content of How to delete multiple files in php. For more information, please follow other related articles on the PHP Chinese website!