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

How to delete all files in php

藏色散人
Release: 2023-03-07 22:38:01
Original
2512 people have browsed it

php method to delete all files: 1. Delete all files contained in the folder through the "deldir($fullpath);" method; 2. Through "public function deldir($dir){...} ” method deletes the folder and all files under the folder.

How to delete all files in php

Recommended: "PHP Video Tutorial"

The operating environment of this tutorial: Windows 7 system, PHP version 7.1, This method works for all brands of computers.

PHP deletes the folder and all files under the folder

1. Only delete the files contained in the folder, not the folder

public function deldir($dir) {
//先删除目录下的文件:
$dh = opendir($dir);
while ($file = readdir($dh)) {
if($file != "." && $file!="..") {
$fullpath = $dir."/".$file;
if(!is_dir($fullpath)) {
unlink($fullpath);
} else {
deldir($fullpath);
}
}
}
closedir($dh);
}
Copy after login

2. Delete the folder and all files under the folder

public function deldir($dir) {
//先删除目录下的文件:
$dh = opendir($dir);
while ($file = readdir($dh)) {
if($file != "." && $file!="..") {
$fullpath = $dir."/".$file;
if(!is_dir($fullpath)) {
unlink($fullpath);
} else {
deldir($fullpath);
}
}
}
closedir($dh);
//删除当前文件夹:
if(rmdir($dir)) {
return true;
} else {
return false;
}
}
Copy after login

3. Create the folder and specify permissions and encoding

if (!is_dir($dir)){                                 //如果目录不存在
mkdir(iconv("UTF-8", "GBK", $dir),0777,true);   //创建目录,777权限,GBK编码格式
}
Copy after login

The above is the detailed content of How to delete all 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