Php directory operations (opendir (), readdir (), closedir)

Source: Internet
Author: User

Rmdir ($ path)
The file with the path in $ path will be deleted .(

First, we will introduce a function that reads data from a directory, such as opendir (), readdir (), and closedir (). When using this function, we will first open the file handle and then list it through iteration:

The code is as follows: Copy code
<? Php
$ Base_dir = "filelist /";
$ Fso = opendir ($ base_dir );
Echo $ base_dir. "While ($ flist = readdir ($ fso )){
Echo $ flist. "<br/> ";
}
Closedir ($ fso)
?>

This is a program that returns the files under the file directory that are already in the Directory (0 files will return false ).

If you need to know the directory information, you can use dirname ($ path) and basename ($ path) to return the directory and file name respectively. disk_free_space ($ path) is available) returns the free space of the viewing space.

Create Command:
 
Mkdir ($ path, 0777)
, 0777 is the permission code, which can be set by the umask () function in a non-window environment.
 

Rmdir ($ path)
The file with the path in $ path will be deleted.

The dir -- directory class is also an important class for operating file directories. It has three methods: read, rewind, and close. This is an object-oriented class. It first uses open file handles, and then read it by pointer ., see the php manual here:
 

The code is as follows: Copy code
<? Php
$ D = dir ("/etc/php5 ");
Echo "handle:". $ d-> handle ."";
Echo "path:". $ d-> path ."";
While (false! ==( $ Entry = $ d-> read ())){
Echo $ entry ."";
}
$ D-> close ();
?>

Output:
 

The code is as follows: Copy code
Handle: resource id #2
Path:/etc/php5
.
..
Apache
Cgi
Cli

File attributes are also very important. File attributes include creation time, last modification time, owner, file Group, type, size, etc.

Next we will focus on file operations.

 

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.