Jump to content

[SOLVED] reading files from folder (strange behaviour)


anatak

Recommended Posts

I am trying to read the files from a folder with the following code

 

$read_path="../public_html/picture/index/original/";
$save_path="../public_html/picture/index/publish/";

if ($handle = opendir($read_path)) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
            echo "<br />$file";
		$pics[]=$file;
        }
    }
    closedir($handle);
}
print_r($pics);
echo '<br />count of pics:'.count($pics);

 

the problem is that it works a bit strange.

I have 4 image files in the folder

and they are found correctly by the script

echo "<br />$file"; shows me the names of the 4 files

 

but for some reason every file is loaded 3 times in the array so I end up with an array of 12 files instead of the 4 I expected.

 

any idea why or what I am doing wrong ?

 

kind regards

anatak

Link to comment
Share on other sites

<?php
$read_path="../public_html/picture/index/original/";
$save_path="../public_html/picture/index/publish/";
$files = glob($read_path."*");
$pics = array();
while ($file = each($files)) {
if (is_file($file = $file['value'])) {
	echo "\n$file";
	$pics[]=$file;
}
}
print_r($pics);
echo '<br />count of pics:'.count($pics);
?>

Link to comment
Share on other sites

i still got double of the results.

 

i tried this it worked alright:

 

$read_path="../public_html/picture/index/original/";
$save_path="../public_html/picture/index/publish/";

if ($handle = opendir($read_path)) {
while (($file = readdir($handle)) !== false)
if ($file != "." && $file != "..") {
echo "<br />$file";
$pics[]=$file;
}
}
closedir($handle);
print_r($pics);
echo '<br />count of pics:'.count($pics);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.