How to use the PHP directory function

Source: Internet
Author: User


This article mainly and everyone to share the PHP directory function use method, hope to help everyone.

Create a Directory

1. mkdir: Try creating a new directory specified by pathname.

BOOL MkDir (string $pathname [, int $mode = 0777 [, bool $recursive = False [, resource $context]])
    • Recursive creation is not supported by default:

    • The third parameter writes true:

Tip: The second one is ignored in Windows and useful in Linux!

Delete Directory

2. RmDir: Try to delete the directory specified by DirName. The directory must be empty and have the appropriate permissions. Failure results in a e_warning level error.

BOOL RmDir (String $dirname [, Resource $context])

Tip: Do not allow deletion of non-empty directories

Get directory Contents

Opendir: Opens a directory handle that can be used for subsequent closedir (), Readdir (), and Rewinddir () calls.

Resource Opendir (String $path [, Resource $context])

Return value: Returns the resource of the directory handle if successful, and FALSE if it fails.

Readdir: Returns the file name of the next file in the directory. The file name is returned as a sort in the file system.

String Readdir ([resource $dir _handle])

Parameter $dir_handle : Resource of the directory handle, previously opened by Opendir ()

Return value: Success returns the file name or FALSE on failure

    • With a handle, read a file (including files and subdirectories) from the directory, read one file at a time, and move the file pointer down!

    • Note that the virtual directory exists under each directory . .. . Represents the current directory, and the parent directory.

With the loop structure, you can get the entire contents of the directory:

It is common to ignore . and ..

0 file names may be treated as false, so you need to use the all-unequal comparison operator to handle

Closedir: Closes the directory stream specified by Dir_handle. The stream must be opened previously by Opendir ().

void Closedir ([resource $dir _handle])

Rename: rename (move) a file or directory and rename the oldname to NewName.

BOOL Rename (string $oldname, String $newname [, Resource $context])

Create a Directory

1. mkdir: Try creating a new directory specified by pathname.

BOOL MkDir (string $pathname [, int $mode = 0777 [, bool $recursive = False [, resource $context]])
    • Recursive creation is not supported by default:

    • The third parameter writes true:

Tip: The second one is ignored in Windows and useful in Linux!

Delete Directory

2. RmDir: Try to delete the directory specified by DirName. The directory must be empty and have the appropriate permissions. Failure results in a e_warning level error.

BOOL RmDir (String $dirname [, Resource $context])

Tip: Do not allow deletion of non-empty directories

Get directory Contents

Opendir: Opens a directory handle that can be used for subsequent closedir (), Readdir (), and Rewinddir () calls.

Resource Opendir (String $path [, Resource $context])

Return value: Returns the resource of the directory handle if successful, and FALSE if it fails.

Readdir: Returns the file name of the next file in the directory. The file name is returned as a sort in the file system.

String Readdir ([resource $dir _handle])

Parameter $dir_handle : Resource of the directory handle, previously opened by Opendir ()

Return value: Success returns the file name or FALSE on failure

    • With a handle, read a file (including files and subdirectories) from the directory, read one file at a time, and move the file pointer down!

    • Note that the virtual directory exists under each directory . .. . Represents the current directory, and the parent directory.

With the loop structure, you can get the entire contents of the directory:

It is common to ignore . and ..

0 file names may be treated as false, so you need to use the all-unequal comparison operator to handle

Closedir: Closes the directory stream specified by Dir_handle. The stream must be opened previously by Opendir ().

void Closedir ([resource $dir _handle])

Rename: rename (move) a file or directory and rename the oldname to NewName.

BOOL Rename (string $oldname, String $newname [, Resource $context])

Related Article

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.