jim.davidson Posted May 24, 2010 Share Posted May 24, 2010 I'm trying to populate a dropdown menu from the names of picture files in a directory. I'm following some code from a book and it's not working. I think it because of the variable $theFolder (line 98 of code). Could someone tell me why this code doesn't work? [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/202734-need-some-help-with-build-array-of-a-folder/ Share on other sites More sharing options...
foxsoup Posted May 24, 2010 Share Posted May 24, 2010 It's possibly failing because the directory doesn't exist ('../Misc' as set on line 72), but hard to tell for sure without knowing the exact error message. If you just want a simple bit of code to scan a directory on the server for image files then this should do the trick though: <?php $dirToScan = '.'; // Directory to scan (current dir) $imgExts = array('jpg', 'jpeg', 'png', 'gif', 'bmp'); // Array of possibly image file extensions $imgFiles = array(); // Create blank results array $allFiles = scandir($dirToScan); // Get all files in dir into an array foreach ($allFiles as $filename) { // Loop through each $fileInfo = pathinfo($filename); // Get the info of each file if (in_array($fileInfo['extension'], $imgExts)) { // Check to see if file ext is in array $imgFiles[] = $filename; // If so, add the filename to results array } } var_dump($imgFiles); // Print the results ?> Quote Link to comment https://forums.phpfreaks.com/topic/202734-need-some-help-with-build-array-of-a-folder/#findComment-1062602 Share on other sites More sharing options...
jim.davidson Posted May 24, 2010 Author Share Posted May 24, 2010 My problem was the way I was describing the folder name. i.e. '../Misc/' Once I looked at your example and chaged my folder name to 'Misc' it works. Thank You for your help Quote Link to comment https://forums.phpfreaks.com/topic/202734-need-some-help-with-build-array-of-a-folder/#findComment-1062612 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.