PHP Directory deletion create traversal

Source: Internet
Author: User
Tags create directory mkdir php tutorial readable strcmp

PHP Tutorial Directory deletion create traversal

Instance directory structure: PHP executable file and leaps tutorial OULCN directory at the same level, created subdirectories in the LEAPSOULCN directory.
Step one: Create related directories using PHP directory functions
Copy code code as follows:
?
mkdir ("LEAPSOULCN", 0777);
mkdir ("Leapsoulcn/leapsoul", 0777);
mkdir ("leapsoulcn/php", 0777);
mkdir ("Leapsoulcn/php/web", 0777);
mkdir ("Leapsoulcn/php/web/test", 0777);
?>

Description: In this code, first use the PHP directory function mkdir to create the home directory LEAPSOULCN, and create two subdirectories, Leapsoul and PHP, in the PHP directory created the web and the test directory.
Knowledge Point: mkdir is primarily used to create directories with two parameters: New directory names (note that you must include directory paths when creating multilevel directories), access to the new directory, that is, umask value, the first number is usually 0, and the second number specifies the owner concession, The third number specifies the concession for the owner user group, the fourth number provides a global concession with values available as follows:
1 = executable
2 = writable
4 = readable
Adds three numbers, 7 represents all permissions, and you can assign a new directory to a different Permissions.
Step Two: Create the Leapsoulcn.txt file in the leapsoulcn/php/directory and write the relevant content
copy code as follows:
.?
@ $fp = fopen ("Leapsoulcn/php/leapsoulcn.txt", "w");
if (! $fp) {
echo "system error";
exit ();
}else {
$filedata = "domain". " T "." Www.111cn.net "." n ";
$filedata = $filedata. " Description "." T "." PHP Web site Development Tutorial Network, PHP Beginners for the PHP tutorial network. "." n ";
$filedata = $filedata. " Title "." T "." This example mainly describes the specific application of PHP directory functions: Cover reading directory, create directory, delete directory and other functions;
Fwrite ($fp, $filedata);
Fclose ($FP);
}

Description: This example code can be used to refer to the introduction of the PHP file to write tutorials.
Step three: Read (traverse) directory name and text file name
Copy code code as follows:
?
$dir = Opendir ("LEAPSOULCN");
while ($filedir = Readdir ($dir)) {
if (!strcmp ($filedir, ".") | |! strcmp ($filedir, "..")) {
Continue
}
echo $filedir. " Directory listing:<br/><br/> ";
$subdir = "leapsoulcn/". $filedir;
$DIRC = "->";
Listsubdir ($subdir, $DIRC);
}
Closedir ($dir);
?>

Description: In this code example tutorial the main use of PHP directory Functions opendir (), Readdir (), Closedir ().
Knowledge Points:
1, the Opendir function is used to open the tour of the specific directory, the function parameter is the directory name, note that because in this example tutorial PHP execution files and tour of the home directory at the same level, so the parameters passed is only directory name, if not at the same level or read multilevel directory, You need to take a specific directory path or file path.
2, after reading the home directory through the Opendir function, through the while loop to further read the multi-level directories and files in the home directory, the PHP directory function used here is Readdir, this function reads the directory or file name from the directory, when there is no readable directory or file, return false, Note that the Read directory contains the. And ..., in this example tutorial because it is a level down reading directory, so when the read directory information is. To continue reading the next level of the table of contents when jumping out of this cycle.
3, after reading all subdirectories and files of the home directory, through the PHP directory function closedir to close the directory handle, similar to the Fclose function to close the file.
Step Fourth: Create recursive functions for read (traversal) directories and files
Copy code code as follows:
?
function Listsubdir ($dirinfo, $DIRC)
{
if (Is_dir ($dirinfo)) {
$subdir = Dir ($dirinfo);
while ($subfile = $subdir->read ()) {
if (!strcmp ($subfile, ".") | |! strcmp ($subfile, "..")) {
Continue
}
$newdir = $dirinfo. " /". $subfile;
if (Is_file ($newdir)) {
Echo $dirc. $subfile. " : File attributes <br/> ";
}
else{
Echo $dirc. $subfile. " : Directory Properties <br/> ";
Listsubdir ($newdir, "-". $dirc);
}
}
$subdir->close ();
Return
}
else return;
}
?>

Note: This function has two parameters: the directory (including directory path) that needs to be read, and displays the multilevel directory separator. In this function, the main use of the PHP file directory function Is_dir,is_file,dir class.
Knowledge Points:
1, first through the is_dir to determine whether to read the directory or file, the function of the parameters and Opendir function Similar, note the directory path problem.
2, if the judge needs to read the directory, then through the Dir directory class to further read its multi-level subdirectories, layer by level recursion. The function functions of the Dir class are the same as those of Opendir, Readdir, Closedir and PHP directory functions.
Now that the entire directory is created, the code instance that reads the directory is complete, listing the multilevel subdirectory names and the text file names under the home directory.
How do I delete a directory?
Delete directory you can use the PHP directory function rmdir, function parameters and mkdir function parameters are similar, you can use the relative directory path or absolute directory path, only the directory to be deleted must be empty directory, through the above code instance you can determine what is empty directory.

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.