PHP reads all file names under the specified directory folder _php tutorial

Source: Internet
Author: User
Traverse the directory under the file name and show that we will use several functions one is the Opendir directory read function, and then use Readdir to get the directory in the information to the array and then through the while traversal.

Show all files in the directory

The code is as follows Copy Code

$dir =dirname (__file__). " /.. /www.bkjia.c0m/";//Enter a different path here
PHP iterates through all the files in the folder
$handle =opendir ($dir. ".");
Defines an array for storing file names
$array _file = Array ();
while (false!== ($file = Readdir ($handle)))
{
if ($file! = "." && $file! = "...") {
$array _file[] = $file; Output file name
}
}
Closedir ($handle);
Print_r ($array _file);

?>


Delete all files under the specified directory

The code is as follows Copy Code


/**
* Delete all files under the specified directory
*
* @param String $dir The path to be manipulated
* Fit range, only for cases where subfolders are not present in the folder
* Source DZ
* Xiao Jia (www.bKjia.c0m) finishing in 2006-06-26
*/
function Dir_clear ($dir) {
$directory = Dir ($dir); Create a Dir class (as Bkjia.c0m's PHP manual says) to read every file in a directory
while ($entry = $directory->read ()) {//loops each file and gets the file name $entry
$filename = $dir. '/'. $entry; Gets the full file name, with the path of the
if (Is_file ($filename)) {//If it is a file, perform the delete operation
@unlink ($filename);
}
}
$directory->close (); To close a class that reads a directory file
Result ();
}

http://www.bkjia.com/PHPjc/633115.html www.bkjia.com true http://www.bkjia.com/PHPjc/633115.html techarticle traverse the directory under the file name and show that we will use several functions one is the Opendir directory read function, and then use Readdir to get the directory in the information to the array and then through the while traversal. ...

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