PHP5-compatible PHP directory management function library _php tips

Source: Internet
Author: User
Tags rewind
Main Compatibility: PHP 5
First, chdir--Change Directory
Syntax: BOOL ChDir (string directory)
return value: Integer
Function Type: File access
Content Description:
Change the current directory of PHP to directory. Directory: The new current directory. The return value returns TRUE if successful, and FALSE if it fails.
Example Explanation:

Program code
<?php
Current directory
Echo GETCWD (). "\ n";
ChDir (' public_html ');
Current directory
Echo GETCWD (). "\ n";
?>


The output results are:
/home/vincent
/home/vincent/public_html

Note: "Warning:chdir (): No such file or directory (errno 2) in * * *" error appears in the Loop statement.


Program code
<?php
Current directory
Echo GETCWD (). "\ n";
For ($i =1 $i <=2; $i + +) {
ChDir (' whoist ');
Current directory
Echo GETCWD (). "\ n";
}
?>


DIR-Directory Class
Syntax: New dir (string directory);
Return value: Class
Function Type: File access
Content Description:
This is an object-oriented class class that is used to read directories. When directory parameter directory is open, there are two properties available: The Handle property is like the Readdir (), Rewinddir (), and Closedir () used by other non-class functions, and the Path property configures the route parameters after opening the directory. This class has three methods: Read, rewind, and close.
Class Dir {
Dir (string directory)
String path
Resource handle
String read (void)
void Rewind (void)
void Close (void)
}
Example Explanation:

Program code
<?php
$d = Dir ("/etc/php5");
echo "Handle:". $d->handle. "\ n";
echo "Path:". $d->path. "\ n";
while (false!== ($entry = $d->read ())) {
echo $entry. " \ n ";
}
$d->close ();
?>


The output results are:
Handle:resource ID #2
Path:/ETC/PHP5
.
..
Apache
Cgi
Cli

Note: The order of the catalog entries returned by the Read method depends on the system.
Note: This function defines the internal class Directory, meaning that the user's own class can no longer be defined with the same name.

Third, Closedir--Close the directory handle
Syntax: void Closedir (Resource dir_handle)
return value: None
Function Type: File access
Content Description:
Closes the directory stream specified by Dir_handle. The stream must be opened before Opendir ().
Example Explanation:

Program code
<?php
$dir = "/etc/php5/";
Open a known directory, read directory into variable and then close
if (Is_dir ($dir)) {
if ($dh = Opendir ($dir)) {
$directory = Readdir ($DH);
Closedir ($DH);
}
}
?>


Iv. Opendir--Open the table of contents handle
Syntax: Resource opendir (string path [, resource context])
return value: Integer
Function Type: File access
Content Description:
This function is used to open the directory data stream. The returned integer is the handle that is available for other directory functions Closedir (), Readdir (), and Rewinddir () operations. If successful, returns the resource of the catalog handle, FALSE if it fails.
Example Explanation:

Program code
<?php
$dir = "/etc/php5/";
Open a known directory, and proceed to read its contents
if (Is_dir ($dir)) {
if ($dh = Opendir ($dir)) {
while (($file = Readdir ($DH))!== false) {
echo "FileName: $file: filetype:". FileType ($dir. $file). "\ n";
}
Closedir ($DH);
}
}
?>


The output results are:
FileName:. : Filetype:dir
FileName:. : Filetype:dir
Filename:apache:filetype:dir
Filename:cgi:filetype:dir
Filename:cli:filetype:dir

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.