Home > Backend Development > PHP Tutorial > Reading files in a directory in PHP_PHP tutorial

Reading files in a directory in PHP_PHP tutorial

WBOY
Release: 2016-07-13 10:59:03
Original
997 people have browsed it

/* purpose: read files in a directory */

//Directory to be read
$folder="H:/temp";

//Open directory
$fp=opendir($folder);

//Read table of contents
while(false!=$file=readdir($fp))
{
//List all files and remove '.' and '..'
if($file!='.' &&$file!='..')
{
//$file="$folder/$file";
$file="$file";

//Assign value to array
$arr_file[]=$file;

}
}
//Output results
if(is_array($arr_file))
{
while(list($key,$value)=each($arr_file))
{
echo "$key=>$value
";
}

}

//Close directory

closedir($fp);

?>


-----------------------
Read files in a directory 2



/* purpose: read files in a certain directory 2*/

function listFiles($dir)
{
//Open directory


$handle=opendir($dir);

//Read table of contents

while(false!=($file=readdir($handle)))

{
//List all files and remove '.' and '..'

if($file!='.'&&$file!='..')

{

//Whether the obtained file name is a directory

if(is_dir("$dir/$file"))

{
//List files in the directory

listFiles("$dir/$file");

}

else

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631972.htmlTechArticle?php /* purpose: Read files in a directory*/ //Directory to be read$ folder=H:/temp; //Open the directory $fp=opendir($folder); //Read the directory while(false!=$file=readdir($fp)) { //List...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template