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

<?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);
?>

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);

Archived

This topic is now archived and is closed to further replies.

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