PHP Directory Operation function Summary _php tutorial

Source: Internet
Author: User
Tags rewind
This article summarizes the PHP directory operation of some functions and use of methods including: Create directory, traverse directory, read directory, close directory, open directory and so on PHP directory operation function, there is a need for friends to refer to.

PHP creates the Directory folder function mkdir (), which is structured as follows:

Kdir (string $dirname, [int $mode])

The parameter $dirname is the name of the directory you want to create, and the parameter $mode is optional, as an integer variable that represents the creation pattern.

Instance:

The code is as follows Copy Code

$name = "PHP";
$d =mkdir ($name, 0777); /* 0777 indicates maximum access rights */
if ($d) {
echo "created successfully";
}
else echo "Creation unsuccessful";
?>


PHP is a lot of ways to traverse the directory, here is a detailed example of PHP traversal directory file method.

Code:

The code is as follows Copy Code
function directory ($DIR) {/* declaring functions */
$DP =opendir ($dir); /* Open Directory */
while ($file =readdir ($DP)) {/* Read directory */
if ($file! = "." && $file! = "...") {/* Determine if there is a "." or ".." FILE */
$path = $dir. "/". $file; /* Get directory path */
if (Is_dir ($path)) {/* Determine if there are subdirectories */
Directory ($path); /* Function Recursive call */
}
else echo $path. "
"; /* Display file */
}
}
Closedir ($DP);
}

Directory ("E:WP");
?>

PHP Read directory function Readdir () can read all the files and folders in this directory, which is structured as follows:


Readdir ($DP);

The parameter $DP the resource object returned by using the function Opendir () to open the directory, and the function returns the file name under the directory.

Instance:

The code is as follows Copy Code

$dir =opendir ("study");
while ($read =readdir ($dir)) {
Print ($read. "
");
}
?>

PHP closes the directory function with Closedir (), which is structured as follows:

Closedir ($DP)

The parameter $DP the resource object returned by using the function Opendir () to open the directory.

Instance:

The code is as follows Copy Code
$mulu = "study";
$dir =opendir ($mulu);
Closedir ($dir);
?>

Using the function Closedir () to close a directory successfully does not return a value of 1, so the IF statement cannot be used to determine whether the closure succeeds

This article introduces a series of PHP file operations, and then the author introduces how to operate the directory. PHP directory function function is similar to the file function, here first introduced the Open Directory function Opendir (), the structure of the form as follows:


Opendir (String $path)


The parameter $path is the path to open the directory, and the function returns a handle to the Open directory to store the current directory resource. Before opening the directory, you must first determine if the directory exists, using the Is_dir () function.

Instance:

The code is as follows Copy Code

if (Is_dir ("Stufdy")) {
Opendir ("Studfy");
Print_r ("Directory opened successfully");
}
Else
echo "Directory does not exist";
?>

The PHP pointer function rewind () sets the file position pointer to the beginning of the file, which is structured as follows:


BOOL Rewind (resource $handle);
The function returns a Boolean value that returns True if successful, and false if it fails.

Instance:

The code is as follows Copy Code


$f =fopen ("Php.txt", "R");
Echo fgets ($f). "
"; /* Output First line */
Echo fgets ($f). "
"; /* Output Second line */
Rewind ($f); /* Pointer returns the file header */
Echo fgets ($f); /* Output First line */
?>

http://www.bkjia.com/PHPjc/628848.html www.bkjia.com true http://www.bkjia.com/PHPjc/628848.html techarticle This article summarizes the PHP directory operation of some functions and use of methods including: Create directory, traverse directory, read directory, close directory, open directory and so on PHP directory operation letter ...

  • 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.