PHP deletes directory and directory file code

Source: Internet
Author: User

Example 1

The code is as follows Copy Code

function Del_dir ($dir) {
{
if (! $dir) {return;}
$cacheDir = $dir;
$DH = Opendir ($cacheDir);
while ($file = Readdir ($DH)) {

if ($file = = '. ') | | ($file = = ' ... ')) {continue;}

if (file_exists ($cacheDir. ') /'. $file)) {
if (Is_dir ($cacheDir. ') /'. $file)) {
Del_dir ($cacheDir. ' /'. $file);
}elseif (!unlink, $cacheDir. ' /'. $file)) {

Finish delete operation
}
}
}
}
}

Example 2

  code is as follows copy code

?
function Deldir ($dir) {
Delete the files in the directory first:
$DH =opendir ($dir);
while ($file =readdir ($DH)) {
if ($file!= "." && $file!= "...") {
$fullpath = $dir. " /". $file;
if (!is_dir ($fullpath)) {
Unlink ($fullpath);
} else {
Deldir ($fullpath);
}
}
}

Closedir ($DH);
Delete current folder:
if (RmDir ($dir)) {
return true;
} else {
return false;
}
}

?>

Example 3

The code is as follows Copy Code

function RemoveDir ($dirName)
{
if (!is_dir ($dirName))//If the incoming parameter is not a directory, it is a file and should be deleted
{
@unlink ($dirName);//delete file
return false;
}
$handle = @opendir ($dirName); If the passed in parameter is a directory, use Opendir to open the directory and assign the returned handle to the $handle
while ($file = @readdir ($handle)!== false)//here explicitly tests whether the return value is equal (both the value and the type) false, otherwise any directory entry's name evaluates to False will cause the loop to stop (for example, a directory named "0 ”)。
{
if ($file!= '. ') && $file!= ' ... ') In the file structure, you will include the form "." and ".." Upward structure, but they are not files or folders
{
$dir = $dirName. '/' . $file; Current file $dir as file directory + file
Is_dir ($dir) RemoveDir ($dir): @unlink ($dir); Determine if the $dir is a directory, and if it is a directory, recursively call the RemoveDir ($dirName) function, delete the files and directories, and delete the file if it is not a directory
}
}
Closedir ($handle);

return rmdir ($dirName);
}

Example 4
Delete a directory created a few days ago

The code is as follows Copy Code

<?php
function Delfile ($dir, $n)//delete all files created by n days before dir path;
{
if (Is_dir ($dir))
{
if ($dh =opendir ($dir))
{
while (false!== ($file = Readdir ($DH)))
{
if ($file!= "." && $file!= "...")
{
$fullpath = $dir. " /". $file;
if (!is_dir ($fullpath))
{
$filedate =date ("y-m-d", Filemtime ($fullpath));
$d 1=strtotime (Date ("y-m-d"));
$d 2=strtotime ($filedate);
$Days =round (($d 1-$d 2)/3600/24);
if ($Days > $n)
Unlink ($fullpath); deleting files

}
}
}
}
Closedir ($DH);
}
}
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.