-1

I have a big images folder with more than 100 000 images. (profile image)

I want to delete every image that is not store in the db (table userimages)

I think that i should scan every file, then get the name of the current file and then, check in my UserImages table if this filename exists... if not...i delete the image.

I found this code, this code is to delete file older than 7 days.. but i will fix it to my needs

$days = 7;  
$path = './logs/';  
$filetypes_to_delete = array("pdf");  

// Open the directory  
if ($handle = opendir($path))  
{  
    // Loop through the directory  
    while (false !== ($file = readdir($handle)))  
    {  
        if (is_file($path.$file))  
        {  
            $file_info = pathinfo($path.$file)
             //check if exist in db

             //if not exist.... i delete the file
        }
    }  
} 

That way, is it good for a folder with a lot of images ? More than 100 000 ?

also i will do 100 000 query to the database.... is it good ?

Is there a better way to do that ?

Thanks a lot !

Pascal

pascal22
  • 73
  • 1
  • 5
  • 1
    Query the database once and retrieve all of the file names and stash them in an array. As you go through the directory see if the name is in the array. If it isn't delete the file. – Dave Apr 18 '19 at 13:56
  • I would dump that table into a flat file on this same system, sorted. Then dump the list of files into a flat file (using `find` or whatever), sort it. Then [compare and see what falls out](https://stackoverflow.com/questions/11165182/difference-between-two-lists-using-bash). Delete what falls out. Querying your database 100000 times is going to be sloooow. – JNevill Apr 18 '19 at 13:56
  • Hello Dave ! Thanks a lot !!! That's what i will do... Query once the database, and check in the array if file exist ! Thanks a lot !!!! – pascal22 Apr 18 '19 at 14:00
  • Thanks JNevil !! I appreciate ! – pascal22 Apr 18 '19 at 14:01

2 Answers2

0

If you are afraid that the database will explode you should make a unique request to the database to take all the file names and then do the above processing. Secondly, use scandir() to get all files names into dir Finally use array_diff() to compare this two arrays and get all differencies.

Alikra
  • 1
  • 1
0

You could store the list of all image files that are in the folder into an $array1. You use that array to make one big sql query to the database, where you get all the images that have one of the file names and store them in an $array2. You walk through this $array2 and create like a $hashmap with the file names as array keys.

Now you got to walk through $array1 and see if the image exists in the db by checking if $hashmap[$filename] exists. If not, delete the image.

Martin Cup
  • 2,399
  • 1
  • 21
  • 32