Home > Backend Development > PHP Problem > How to delete multiple files in php

How to delete multiple files in php

藏色散人
Release: 2023-03-08 11:56:01
Original
1642 people have browsed it

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.

How to delete multiple files in php

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);
}
}
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template