anatak Posted October 5, 2009 Share Posted October 5, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/176493-solved-reading-files-from-folder-strange-behaviour/ Share on other sites More sharing options...
RussellReal Posted October 5, 2009 Share Posted October 5, 2009 <?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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/176493-solved-reading-files-from-folder-strange-behaviour/#findComment-930353 Share on other sites More sharing options...
ProXy_ Posted October 5, 2009 Share Posted October 5, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/176493-solved-reading-files-from-folder-strange-behaviour/#findComment-930356 Share on other sites More sharing options...
anatak Posted October 5, 2009 Author Share Posted October 5, 2009 thank you now it works perfectly. I guess this caused by not using closedir() ? Quote Link to comment https://forums.phpfreaks.com/topic/176493-solved-reading-files-from-folder-strange-behaviour/#findComment-930371 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.