Read files in a directory in PHP

Source: Internet
Author: User

<?php
/*
PHP version:28 April 2005
Author:move2rent
Purpose: Read a file in a directory
*/

The directory to read
$folder = "H:/temp";

Open Directory
$FP =opendir ($folder);

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

assigning to arrays
$arr _file[]= $file;

}
}
Output results
if (Is_array ($arr _file))
{
while (list ($key, $value) =each ($arr _file))
{
echo "$key => $value <br>";
}

}

Close Directory

Closedir ($FP);

?>


----------------------
Read file 2 in a directory


<?php

/*php version:30 April 2005
Author:move2rent
Purpose: Read the file in a directory two */


/*----------------------------------*/

function Listfiles ($dir)
{
Open Directory


$handle =opendir ($dir);

Reading Table of Contents

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

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

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

{

Whether the resulting file name is a directory

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

{
Listing files in a directory

Listfiles ("$dir/$file");

}

Else

{
Open the file if it is a file


$FP =fopen ("$dir/$file", "R");


Read the contents of the file

$data =fread ($fp, FileSize ("$dir/$file"));

if ($data)

Assign the read to an array

$file _array[]= "$dir/$file";

/*foreach ($file _array as $key => $value)
{
echo "$value <br>";
}
*/
echo Count ($file _array);

Output results

while (list ($key, $value) =each ($file _array))
{
echo "$key => $value <br>";
}
}

}
}
}

/*------------------------------------------*/
Call

$dir = "H:/temp";

Listfiles ($dir);

?>


-----------------------------------------------
Create the directory if it does not exist


<?php

/*
PHP version:28 April 2005
Author:move2rent
Purpose: Reads a file in a directory and creates it if it does not exist
*/

$folder = "H:/TEMP1";

Open Directory
@ $fp =opendir ($folder);

Create the directory if it does not exist
if (! $fp)
{

Create a table of contents
mkdir ($folder);

Modify permissions for this directory
chmod ($folder, ' 0755 ');

echo "The folder create Success!";

}

Else
{
echo "The folder have exists."
}

?>



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.