PHP Development File Directory processing

Source: Internet
Author: User

1. Open/close file directory
Opening/Closing the file directory is similar to opening/closing files, but if the open file does not exist, a file is created automatically. If the path to the open file is incorrect, an error will be taken.
(1) Open Directory
PHP uses the Opendir () function to open the directory with the following syntax:
Resource Opendir (string path)
Path is a valid directory path and returns a directory pointer after successful execution. Returns False if path is not a valid directory path or cannot be opened because of a permission or system error. and generates an error warning. The output of the error message can be suppressed by adding an ' @ ' character to the front of the Opendir ().

(2) Close the directory
Close the directory using the Closedir () function with the following syntax:
void Closedir (Resource handle)
The parameter handle is a directory pointer returned by the OPENDIR () function.

The sample code for the open/close directory is as follows:

<?php$filepath='/applications/xampp/xamppfiles/htdocs/phptest/';if(Is_dir ($filepath)) {//Detect if it is a directory    if($dir=opendir ($filepath)) {Echo "The directory is as follows:";Echo $dir; }Else{Echo "<<<< Directory Error";Exit(); } closedir ($dir);}Else{Echo "Directory Error >>>";}?>

The results of the operation are as follows:

(3) Browse the catalogue
The browse directory in PHP uses the Scandir () function, with the following syntax:
Array Scandir (string Diretory[,int Sorting_order])
The function returns all the files and directories under the directory, the parameter sorting_order specifies the sort order, and the default alphabetical order. If parameters are added, they are sorted in descending order.

The sample code is as follows:

<?php   $filepath  = '/applications/xampp/xamppfiles/htdocs/phptest/' ; if  (Is_dir ( $filepath )) {//detect if it is a directory   $dir  =scandir ( $filepath ); foreach  ( $dir  as   $value ) {echo   $value .  ' <br> ' ; }}else  {echo   "directory error >>> ";} ?>   

The results of the operation are as follows:

The following table is a common directory manipulation function:

PHP Development File Directory processing

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.