Jump to content

files in directory


dreamwest

Recommended Posts

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?

Link to comment
Share on other sites

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!");

Link to comment
Share on other sites

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>";

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

$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>";
}

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.