Jump to content

Recommended Posts

I have a database that stores events and the event submission works fine. I do a cron to delete all the events that have ended. But now i'm developing an image upload feature for an event. So...someone creates an event and now they have the option to upload a picture to accompany it. As the pictures are completely separate from my database, i can't figure out the best way to delete them. Cause i want the pictures to be deleted along with the expired events and a cron would be excellent if i figure the theory out. Anybody have any ideas?

 

Link to comment
https://forums.phpfreaks.com/topic/166864-solved-delete-images-with-cron/
Share on other sites

This might help..

The images connected to an event can be named in some particular format involving the unique eventid say <eventid>_name_<i>.jpg or some like that, so that while we are deleting the expired events we can check for the images named after that event id and delete those images.

not sure i follow. i get what you're saying about the naming of the image. but not sure how i would search them...heres my simple cron delete script

 

<?php
$cxn = mysqli_connect($host,$user,$passwd,$dbname)
          or die ("Couldn't connect");
$query = "DELETE FROM events WHERE end < NOW()";
$rsult = mysqli_query($cxn,$query)
          or die (mysqli_error($cxn));

?>

 

 

 

Since the image names are connected to the eventid, we can take a list of event ids to be deleted. by 'select eventid from events....'

if we know the path of the image location, then using glob function http://in2.php.net/manual/en/function.glob.php we can get the list of images related to each of the event id (may be we can use '<event_id>_name*' for the glob filter) and unlink those images

ok i think im starting to see...i didn't know that glob function existed. So i guess, to make things easier, i could just rename the images that the people upload to be 'the-event-id-number.png'.

 

i so then i would...

 

1. select all expired events and grab their id numbers

2. search through the directory with those id-numbers.png

3. unlink them

 

step 2 is a little blurry to me though

 

After step 1 i would have an array with id numbers...but how would use that array of just numbers(i.e. 3123, 2342, 2324) to search through and delete those found in the directories with those id numbers that have .png attached to them?

some thing like this should do it

 

$idarray = array(3123, 2342, 2324) // array of ids
chdir(path-to-images);
foreach($idarray as $id) {
    $filelist = glob("$id_*");// should list all files starting with $id_ ( according to your image naming logic )
   foreach( $filelist as $file) {
     unlink(path-to-image-folder/$file);
   }
  
}

i'm thinking about naming the images after the timestamp that they end at. So if the event ends now it would be something like "1248218825admin3433.png". The long number would be the ending timestamp then the creator then the event id. This would make a unique image name.

 

I could then use a function(that i have no idea how to write) to grab that front timestamp and check to see if that event and its image has expired...if so then unlink.

 

This way the image deleting cron could run without relying on the database at all. Your thoughts??

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.