-2

I want to delete images from a specific category that are older than a certain age (say, 2 months).

These images are located in directory somewhere, if I use the following code:

<?php

// define the directory
$dir = "images/";

// cycle through all files in the directory
foreach (glob($dir."*") as $file) {

    // if file is 2 Month (5.184e+6 seconds) old then delete it
    if (filemtime($file) < time() - 5.184e+6) {
        unlink($file);
    }
}

?>

Then it deletes all 2 month old images, but I want to delete images by category.

This is a table in my database:

------------------------------------
table msn_story_images
------------------------------------
ID    image(url)   thumbnail   sortstyID

The images folder is pretty large (around 19GB), and can't be listed on the server.

Amelia
  • 2,967
  • 2
  • 24
  • 39
Hafiz Usman aftab
  • 300
  • 2
  • 5
  • 15

2 Answers2

2

OK, so you're saving only the image URL in the database. I guess you can have a logical relation between db images and file system images by the images' name. I would suggest to get the relevant images from the DB first of all, and put them into a flat array containing only the URLs. Go through the array and convert the image names to omit the leading URL part. A simplified example:

foreach($imagesWithUrl as $idx => $image) {
  $imagesWithUrl[$idx] = basename($image);
}

Then, in your loop, check for existance of this image in the array coming from DB:

/*** cycle through all files in the directory ***/
foreach (glob($dir."*") as $file) {

  /*** if file is 2 Month (5.184e+6 seconds) old then delete it ***/
  if (filemtime($file) < time() - 5.184e+6 && in_array(basename($file), $imagesWithUrl)) {
      unlink($file);
  }
}
Legionar
  • 7,472
  • 2
  • 41
  • 70
jossif
  • 399
  • 2
  • 10
  • can i apply it online any risk factor ? – Hafiz Usman aftab Oct 05 '15 at 16:22
  • sure, you can. to eliminate major risks, always test your code locally or in your dev environment. further, when applying delete jobs, make sure that the system user who's about to delete has only write permissions to the target folder, not to parent folders. – jossif Oct 05 '15 at 16:59
  • your code run good and i show my script that was successfully run – Hafiz Usman aftab Oct 07 '15 at 10:15
0

I make this script for my question and that run perfectly if more improvemense then tell me

   <?php
$conn = new mysqli('localhost', 'freemed_mainuser', 'xDPf$$!O!H61','freemed_main');


$sql="select img.styid imgid,concat('sam_data/story/images/',img.image) as img,story.id storyid,story.title storytitle,story.insdatetime insdatetime from msn_story_images img , msn_stories story WHERE  img.styid=story.id and insdatetime < DATE_SUB( CURDATE( ) , INTERVAL 60 
DAY ) 
AND catid NOT 
IN ( 1, 86, 85, 88, 87 )
";
$data = mysqli_query($conn,$sql);


while($row=mysqli_fetch_array($data)){

    unlink($row['img']);

    echo "files deleted";



}

/*and delete matching rows from DB*/ 
$del="DELETE msn_story_images,
msn_stories FROM msn_stories INNER JOIN msn_story_images WHERE msn_story_images.styid = msn_stories.id AND msn_stories.insdatetime < DATE_SUB( CURDATE( ) , INTERVAL 60 DAY ) AND msn_stories.catid NOT IN ( 1, 86, 85, 88, 87 )";
$del_data=mysqli_query($conn,$del);


?>
Hafiz Usman aftab
  • 300
  • 2
  • 5
  • 15