Solved

PHP delete smaller files

Posted on 2013-06-29
13
200 Views
Last Modified: 2013-06-29
Hello,

I am not sure if this is possible but I am optimistic.

I have an images folder with 75,000 images. I had a db that separated them as origina/small/thumb but I lost the db.

I now have one images folder with 3 versions of each file and I need to strip out the small and thumbs and just keep the originals.

Is there a way to query the folder and keep the largest file size of each filename and delete the other 2?

example

1221-93939a-280-510.jpg
1221-93939a-180-180.jpg
1221-93939a-580-720.jpg


175753-a344va-280-500.jpg
175753-a344va-180-160.jpg
175753-a344va-580-735.jpg

I used this code to echo the file names in the folder.

<?php
 foreach(glob('./images/*.*') as $filename){
     echo $filename;
 } 
?>

Open in new window

0
Comment
Question by:movieprodw
  • 8
  • 5
13 Comments
 
LVL 58

Expert Comment

by:Gary
ID: 39287064
Whats the common denominator to identify the image groups?
Is it this bit 175753-a344va and 1221-93939a
Is there something in the filename to identify the size?
0
 
LVL 1

Author Comment

by:movieprodw
ID: 39287070
the common pattern would be the -xxx-xxx.jpg
0
 
LVL 58

Expert Comment

by:Gary
ID: 39287089
Hmmm, the examples you gave xxx-xxx,jpg is not common this bit is 175753-a344va.
Also asking do the numbers 280/500, 180/160, 580/735 represent the sizes or something in this block.

175753-a344va-280-500.jpg
175753-a344va-180-160.jpg
175753-a344va-580-735.jpg
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:movieprodw
ID: 39287099
Yeah, well the are in that pattern but they do not always match, the beginning number does.

for example I would like to delete all that are ending in

-320-xxx.jpg
0
 
LVL 1

Author Comment

by:movieprodw
ID: 39287107
I found this, not sure if it helps.

// Get a list of all CSV files in your folder.
$csv = glob("*.csv");

// Sort them by modification date.
usort($csv, function($a, $b) { return filemtime($a) - filemtime($b); });

// Remove the newest from your list.
array_pop($csv);

// Delete all the rest.
array_map('unlink', $csv);

Open in new window

0
 
LVL 58

Expert Comment

by:Gary
ID: 39287109
You've lost me now, you have 3 images - thumb/small/original - what part of the filename is the same for all 3 images but unique from other images?
Is the 280,180,580 in the filenames something to do with the size - these seem to be consistent in your filenames.

x-x-280-510.jpg
x-x-180-180.jpg
x-x-580-720.jpg
0
 
LVL 1

Author Comment

by:movieprodw
ID: 39287119
Hello,

Yes they are the file size, the width is consistent but the height is not.

Sorry for being confusing.

Ideally it could grab the 'x' part and compare it to the 'y' part then delete the smaller and keep the larger. xxxxxx-xxxxxx-yyy-yyy.jpg

But if you have a way to delete by pattern such as explode filename and unset if $filename[2] = '280' then that would be great too.

Matt
0
 
LVL 58

Expert Comment

by:Gary
ID: 39287141
Just to be sure
x-x-280-510.jpg

the 280 represents the width and the 510 is the height

All images have a width of either
180, 280 or 580px

So we can delete all images with a width of 180 or 280px

<?php
$dir = "images/*";  
  
$pattern1 = '/.*?(180)([-+]\d+)(.)(jpg)/';
$pattern2 = '/.*?(280)([-+]\d+)(.)(jpg)/';

foreach(glob($dir) as $file)

{
if(preg_match($pattern1, $file) || preg_match($pattern2, $file)){

     echo $file.'<br>';
}

}

Open in new window


This doesn't delete the files - I just want you to test it shows the correct files for deletion
0
 
LVL 1

Author Comment

by:movieprodw
ID: 39287147
That does show the correct images to delete.
0
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39287158
If you are absolutely sure then replace
echo $file.'<br>';

Open in new window

with
unlink($file);

Open in new window


You definitely sure...? ;o)
0
 
LVL 1

Author Closing Comment

by:movieprodw
ID: 39287160
Thank you Gary, you are always very helpful.

I have a backup so if I mess up I will reinstate it.

Thanks again.
Matt
0
 
LVL 1

Author Comment

by:movieprodw
ID: 39287290
I ran the script and it does not unlink them, could their be a permissions error?

Thanks
0
 
LVL 1

Author Comment

by:movieprodw
ID: 39287296
Got it, it was a permissions error.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question