Create multilevel directories and delete multilevel directories and files in PHP

Source: Internet
Author: User
Tags mkdir

mkdir in PHP create multilevel catalogs

The code is as follows Copy Code
function Mkdirs ($dir)
{if (!is_dir ($dir))
{if (!mkdirs (DirName ($dir)))
{return false; }
if (!mkdir ($dir, 0777))
{return false; }  }
return true;
}

Call method

Mkdirs (' div/css/layout ');


A more standard way to create multilevel catalogs

The code is as follows Copy Code

Check and create multilevel catalogs
function Checkdir ($path) {
$pathArray = explode ('/', $path);
$nowPath = ';
Array_pop ($pathArray);
foreach ($pathArray as $key => $value) {
if (' = = $value ') {
Unset ($pathArray [$key]);
}else{
if ($key = = 0)
$nowPath. = $value;
Else
$nowPath. = '/'. $value;
if (!is_dir ($nowPath)) {
if (!mkdir ($nowPath, 0777)) return false;
}
}
}
return true;
}

Delete a multilevel directory method

Test successfully under WinXP, as long as the PHP file encoding for gb2312, file name arbitrary, should be changed to encode the file name gb2312, on the line, no test

The code is as follows Copy Code

<?php
Header ("content-type:text/html; charset=gb2312 ");
if (Deletedir ('./complex complex) complex complex complex complex parts duplicate complex duplicate piece complex)
echo "Delete Success";
function Deletedir ($dir)
{
if (@rmdir ($dir) ==false && is_dir ($dir))//deleted, go to delete all files
{
if ($DP = Opendir ($dir))
{
while (($file =readdir ($DP))!= false)
{
if ($file!= '. ' && $file!= ' ... ')
{//echo $file = $dir. '/'. $file; Echo ' <br/> ';
$file = $dir. '/'. $file;
if (Is_dir ($file))//is the real directory
{
Deletedir ($file);
}else {
Unlink ($file);
}
}
}
Closedir ($DP);
}else
{
return false;
}
}
if (Is_dir ($dir) && @rmdir ($dir) ==false)//is directory is not deleted
return false;
return true;
}
?>

Recursively Delete multilevel directories

In the same vein, PHP uses RmDir and unlink to recursively delete the multilevel directory code:

  code is as follows copy code

function Rmdirs ($dir)  

$d = Dir ($dir); 
while (false!== ($child = $d->read ()) { 
if ( $child!= '. ' && $child!= '. ')

if (Is_dir ($dir. '/$child))  
Rmdirs ($dir. '/'. $child); 
Else
unlink ($dir. '/'. $child); } 

$d->close ();

The

//Call method is also very simple as long as the directory is good  
RmDir ($dir);
}

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.