dreamwest Posted June 12, 2009 Share Posted June 12, 2009 I have differnt images in a directory like this: 3000.jpg 1_3000.jpg 2_3000.jpg 3_3000.jpg I want to recreate all images with the 1_ 2_ and 3_ extension but leave the main image 3000.jpg How can i select only these images and leave the 3000.jpg image? Quote Link to comment https://forums.phpfreaks.com/topic/161936-files-in-directory/ Share on other sites More sharing options...
waynew Posted June 12, 2009 Share Posted June 12, 2009 if(stristr($filename,"_")){ //do whatever - assuming of course that the regular images wont have an underscore } Quote Link to comment https://forums.phpfreaks.com/topic/161936-files-in-directory/#findComment-854408 Share on other sites More sharing options...
rhodesa Posted June 12, 2009 Share Posted June 12, 2009 $dir = 'path/to/images/'; foreach(scandir($dir) as $file){ if(!preg_match('/^\d+_/',$file)) continue; //Skip anything that doesn't start with a #_ echo "$dir$file<br>"; } Quote Link to comment https://forums.phpfreaks.com/topic/161936-files-in-directory/#findComment-854409 Share on other sites More sharing options...
PFMaBiSmAd Posted June 12, 2009 Share Posted June 12, 2009 <?php $files = glob('?_*.jpg'); echo "<pre>",print_r($files,true),"</pre>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/161936-files-in-directory/#findComment-854439 Share on other sites More sharing options...
Anti-Moronic Posted June 12, 2009 Share Posted June 12, 2009 Gotta say, I absolutely love glob for stuff like this. You may not need it, but it gives you all the power of regex within a very simple function. Class! Quote Link to comment https://forums.phpfreaks.com/topic/161936-files-in-directory/#findComment-854501 Share on other sites More sharing options...
dreamwest Posted June 12, 2009 Author Share Posted June 12, 2009 OK all the examples work but im trying to integrate it with my script - im lost. Heres what ive got at the moment: // connect to source ftp server $source_conn = ftp_connect($source_ftp_server) or die("Could not connect to $source_ftp_server!"); ftp_login($source_conn, $source_ftp_user, $source_ftp_pass) or die("Could not login to $source_ftp_server!"); // get a list of all files and pick a random one $source_file_list = ftp_nlist($source_conn, "."); do { shuffle($source_file_list); $our_file = $source_file_list[0]; } while (strtolower(substr($our_file, -4)) != '.jpg'); // get that file ftp_get($source_conn, "tmp-" . $our_file, $our_file, FTP_BINARY) or die("Could not download $our_file from $source_ftp_server!"); Quote Link to comment https://forums.phpfreaks.com/topic/161936-files-in-directory/#findComment-854744 Share on other sites More sharing options...
.josh Posted June 12, 2009 Share Posted June 12, 2009 Gotta say, I absolutely love glob for stuff like this. You may not need it, but it gives you all the power of regex within a very simple function. Class! Not quite all the power of regex. But a decent amount nonetheless. Read the user notes in the manual. Quote Link to comment https://forums.phpfreaks.com/topic/161936-files-in-directory/#findComment-854750 Share on other sites More sharing options...
dreamwest Posted June 13, 2009 Author Share Posted June 13, 2009 OK ive manged to recreate 20,000 images but the script times out, i have over 100,000 images in this directory is there a way i can only select files that are over 8kb in size, i tried this but its only selecting images 3000.jpg instead of 1_3000.jpg $dir = '/home/user/public_html/thumb/'; foreach(scandir($dir) as $file){ $filesize = round(filesize($file)/1024); if(!preg_match('/^\d+_/',$file) && $filesize > continue; //Skip anything that doesn't start with a #_ echo "$file<br>"; Quote Link to comment https://forums.phpfreaks.com/topic/161936-files-in-directory/#findComment-854849 Share on other sites More sharing options...
xtopolis Posted June 13, 2009 Share Posted June 13, 2009 $dir = '/home/user/public_html/thumb/'; $files = glob($dir . '?_*.jpg'); foreach($files as $file){ if( (round(filesize($file)/1024) ) > 8 ) { //do things with this file } } This is assuming that the glob correctly selects the files you intend, and your rounding is correct. Haven't tested. You may have to modify the glob function to accommodate capitalization of file extensions. See the manual for more info. Quote Link to comment https://forums.phpfreaks.com/topic/161936-files-in-directory/#findComment-854868 Share on other sites More sharing options...
dreamwest Posted June 13, 2009 Author Share Posted June 13, 2009 $dir = '/home/user/public_html/thumb/'; $files = glob($dir . '?_*.jpg'); foreach($files as $file){ if( (round(filesize($file)/1024) ) > 8 ) { //do things with this file } } This is assuming that the glob correctly selects the files you intend, and your rounding is correct. Haven't tested. You may have to modify the glob function to accommodate capitalization of file extensions. See the manual for more info. Thanks for the help. Ive figured it out, and successfully make new -smaller thumbs in the directory. But i have one final step, i need to set it to cron and run it once a day but limit the files it selects to something like 30 files, or a time range like select files modifided today only require_once('../include/SimpleImage.class.php'); $dir = '/home/user/public_html/thumb/'; foreach(scandir($dir) as $file){ if(!preg_match('/^\d+_/',$file)) continue; //Skip anything that doesn't start with a #_ $filesize = round(filesize($dir."".$file)/1024); if ($filesize > 6){ echo "<hr>".$file." Image has size of ".$filesize." - NOW RESIZING <hr>"; // resize $image = new SimpleImage(); $image->load($dir."".$file); $image->resize(140,110); $image->save($dir."".$file); }else{ echo "".$file." SKIPPING image has size of ".$filesize."<br>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/161936-files-in-directory/#findComment-854878 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.