jacko310592 Posted November 30, 2009 Share Posted November 30, 2009 hey guys, can anyone suggest a way to make to following line of code look for files with more than just 'jpg' within the file name (eg 'jpg, JPG, JPEG, jpeg') $files = grab_files('./gallery', 'jpg'); thanks everyone Quote Link to comment https://forums.phpfreaks.com/topic/183396-grab_files-multiple-extension/ Share on other sites More sharing options...
trq Posted November 30, 2009 Share Posted November 30, 2009 Considering grab_files() is not a php function, no. You could make the grab_files function except an array of files to look for. Quote Link to comment https://forums.phpfreaks.com/topic/183396-grab_files-multiple-extension/#findComment-968001 Share on other sites More sharing options...
jacko310592 Posted November 30, 2009 Author Share Posted November 30, 2009 oh okay, no wonder i cant find anything on the net about it XD Quote Link to comment https://forums.phpfreaks.com/topic/183396-grab_files-multiple-extension/#findComment-968006 Share on other sites More sharing options...
jacko310592 Posted November 30, 2009 Author Share Posted November 30, 2009 im not very good with arrays =/ should it be somthing like this?.. $fileType = Array('jpg', 'JPG', 'jpeg', 'JPEG'); $files = grab_files('./gallery', ''.$fileType.''); Quote Link to comment https://forums.phpfreaks.com/topic/183396-grab_files-multiple-extension/#findComment-968010 Share on other sites More sharing options...
trq Posted November 30, 2009 Share Posted November 30, 2009 The syntax would be.... $fileType = Array('jpg', 'JPG', 'jpeg', 'JPEG'); $files = grab_files('./gallery', $fileType); I'm not sure where people get the idea that variables need quotes around them. However.... You can't just hand any old function an array and expect it to work. You would likely need to customize the function to work with an array is input. Quote Link to comment https://forums.phpfreaks.com/topic/183396-grab_files-multiple-extension/#findComment-968016 Share on other sites More sharing options...
salathe Posted November 30, 2009 Share Posted November 30, 2009 In future, please either post the source of the function (in the thread, in a pastebin or as a link to it: e.g. http://www.phpfreaks.com/forums/index.php?topic=278778.0) so that we can see how it works, since it's not a core PHP function. For this particular issue (accepting an array of allowable extensions) the line reading if(pathinfo($file, PATHINFO_EXTENSION) == $ext) inside the grab_files function will need to be amended to compare the file extension against the provided array. Quote Link to comment https://forums.phpfreaks.com/topic/183396-grab_files-multiple-extension/#findComment-968053 Share on other sites More sharing options...
jacko310592 Posted November 30, 2009 Author Share Posted November 30, 2009 code still doesnt work even without the quotes =[ nd hey salathe, this is the source of the function: <?php $start_dir = "./gallery_and_purchase"; function grab_files($start_dir, $ext) { $dir = new RecursiveDirectoryIterator($start_dir); $files = Array(); foreach($dir->getChildren() as $file) if (stristr($file, "large")===FALSE) if(is_dir($file)) $files = array_merge($files, grab_files($file, $ext)); else if(pathinfo($file, PATHINFO_EXTENSION) == $ext) $files[] = (string) $file; return $files; } $fileType = Array('jpg', 'JPG', 'jpeg', 'JPEG'); $files = grab_files('./gallery', $fileType); shuffle($files); array_splice($files, 15); foreach($files as $file) { $file = str_replace("\\.\\","/",$file); $fileSplit = explode("/", $file); $fileName = $fileSplit[3]; $fileName = substr("$fileName", 0, -4); echo $fileName; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/183396-grab_files-multiple-extension/#findComment-968058 Share on other sites More sharing options...
trq Posted November 30, 2009 Share Posted November 30, 2009 code still doesnt work even without the quotes =[ No kidding. Its not written to accept an array. Quote Link to comment https://forums.phpfreaks.com/topic/183396-grab_files-multiple-extension/#findComment-968080 Share on other sites More sharing options...
jacko310592 Posted November 30, 2009 Author Share Posted November 30, 2009 well in that case, can anyone think of a way this can be done if arrays cant be used? Quote Link to comment https://forums.phpfreaks.com/topic/183396-grab_files-multiple-extension/#findComment-968221 Share on other sites More sharing options...
jacko310592 Posted November 30, 2009 Author Share Posted November 30, 2009 SOLVED: <?php $start_dir = "./gallery"; function grab_files($start_dir, $ext) { $dir = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($start_dir)); $files = array(); // Force array of extensions and make them all lower-case if ( ! is_array($ext)) { $ext = (array) $ext; } $ext = array_unique(array_map('strtolower', $ext)); foreach($dir as $file) { // Skip anything that isn't a file if ( ! $file->isFile()); continue; // If the file has one of our desired extensions, add it to files array if (in_array(strtolower(pathinfo($file->getFilename(), PATHINFO_EXTENSION)), $ext)) { $files[] = $file->getPathname(); } } return $files; } // Grab all .jpg files recursively $fileType = Array('jpg', 'jpeg'); $files = grab_files('./gallery', $fileType); // Get random keys $keys = array_rand($files, min(20, count($files))); foreach($keys as $key) { echo $files[$key] . "<br>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/183396-grab_files-multiple-extension/#findComment-968245 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.