This article summarizes the PHP directory operation method. Share to everyone for your reference, specific as follows:
Directory Operations
new directory : mkdir (path, permissions, recursive creation)
Delete directory : RmDir ()
Mobile (renamed): rename ()
Get directory Contents :
Open Directory
Directory handle = Opendir ()
Reading directories
filename = readdir (directory handle)
Read the file name in turn, move the handle pointer down, and return false when it is not read
Close Directory
Closedir ()
Recursively read Directory contents:
<?php
Showdir ('.. /.. /file ');
function Showdir ($path, $dep =0) {
$pos = Opendir ($path);
while (false!== $file =readdir ($pos)) {
if ($file = = '. ') $file = = '.. ') Continue;
echo str_repeat ("", $dep *4), $file. ' </br> ';
if (Is_dir ($path. ') /'. $file)) {
$func = __function__;
$func ($path. ' /'. $file, $DEP + 1);}}
The Operation effect chart is as follows:
<?php
$res = Showdir ('.. /.. /file ');
Echo ' <pre> ';
Print_r ($res);
function Showdir ($path) {
$pos = Opendir ($path);
$next = Array ();
while (false!== $file =readdir ($pos)) {
if ($file = = '. ') $file = = '.. ') Continue;
$fileinfo = Array ();
$fileinfo [' name '] = $file;
if (Is_dir ($path. ') /'. $file)) {
$fileinfo [' type '] = ' dir ';
$func = __function__;
$fileinfo [' next '] = $func ($path. ') /'. $file);
} else{
$fileinfo [' type '] = ' file ';
}
$next [] = $fileinfo;
}
Closedir ($pos);
return $next;
}
The Operation effect chart is as follows:
To delete a directory recursively:
<?php
Showdir ('.. /.. /file/sim ');
function Showdir ($path, $dep =0) {
$pos = Opendir ($path);
while (false!== $file =readdir ($pos)) {
if ($file = = '. ') $file = = '.. ') Continue; Echo Str_repeat ("", $dep *4), $file. ' </br> ';
if (Is_dir ($path. ') /'. $file)) {
$func = __function__;
$func ($path. ' /'. $file, $DEP + 1);
} else{
unlink ($path. ' /'. $file);
}
RmDir ($path);
Closedir ($pos);
Directory file Encoding problem:
When presented, converts the operating system code into a response data encoding
Windows for GBK, Project Utf-8
Iconv (' GBK ', Utf-8 ', file);
Code address in Chinese: need to convert to system encoding
Iconv (Utf-8 ', ' GBK ', file);
More about PHP Interested readers can view the site topics: "PHP Directory Operation tips Summary", "PHP file Operation Summary", "PHP Array" operation Skills Encyclopedia, "PHP Basic Grammar Introductory Course", "PHP operation and operator Usage Summary", " Introduction to PHP object-oriented programming program, "PHP Network Programming Skills Summary", "PHP string (String) Usage Summary", "Php+mysql database Operation Tutorial" and "PHP common database Operation Skills Summary"
I hope this article will help you with your PHP programming.