bassguru Posted September 12, 2009 Share Posted September 12, 2009 Hello everyone, I have some code that reads from a specified directory and puts the files into an array. The problem is that if there are no files in the directory, then the array is undefined and error messages start appearing all over my site. //Retrieve files that are of a type matching those specified while($imgfile = readdir($dimg)) { if(in_array(strtolower(substr($imgfile,-3)),$fileTypes)) { $userImage[] = $imgfile; sort($userImage); reset ($userImage); } } The 'undefined' array would be $userImage. How do I solve this problem? Many thanks in advance bassguru Link to comment https://forums.phpfreaks.com/topic/173988-solved-readdir-problems/ Share on other sites More sharing options...
DEVILofDARKNESS Posted September 12, 2009 Share Posted September 12, 2009 You can first try to find out if the dir isset.. Link to comment https://forums.phpfreaks.com/topic/173988-solved-readdir-problems/#findComment-917161 Share on other sites More sharing options...
bassguru Posted September 12, 2009 Author Share Posted September 12, 2009 I already have validation for that. The directories are unique to each user of the site. They are there to save the user's uploaded images. When users first create an account, there are no images (thus no files) in the directory. Also, the user can delete the image files in the directory at anytime, so I really need something that will solve the problem of an undefined array variable. Link to comment https://forums.phpfreaks.com/topic/173988-solved-readdir-problems/#findComment-917166 Share on other sites More sharing options...
bassguru Posted September 12, 2009 Author Share Posted September 12, 2009 On second thoughts, you are completely right! if (isset($userImage)) { //Display the images etc... } If there are no images, no need to use the array therefore error is not declared! Thanks bassguru Link to comment https://forums.phpfreaks.com/topic/173988-solved-readdir-problems/#findComment-917178 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.