dadamssg Posted July 21, 2009 Share Posted July 21, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/166864-solved-delete-images-with-cron/ Share on other sites More sharing options...
webbnino Posted July 21, 2009 Share Posted July 21, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/166864-solved-delete-images-with-cron/#findComment-879855 Share on other sites More sharing options...
dadamssg Posted July 21, 2009 Author Share Posted July 21, 2009 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)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/166864-solved-delete-images-with-cron/#findComment-879858 Share on other sites More sharing options...
webbnino Posted July 21, 2009 Share Posted July 21, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/166864-solved-delete-images-with-cron/#findComment-879861 Share on other sites More sharing options...
dadamssg Posted July 21, 2009 Author Share Posted July 21, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/166864-solved-delete-images-with-cron/#findComment-879872 Share on other sites More sharing options...
webbnino Posted July 21, 2009 Share Posted July 21, 2009 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); } } Quote Link to comment https://forums.phpfreaks.com/topic/166864-solved-delete-images-with-cron/#findComment-879882 Share on other sites More sharing options...
dadamssg Posted July 21, 2009 Author Share Posted July 21, 2009 i've never seen "$id_*"...what exactly is that? does that look for files with those certain numbers at the beginning and doesn't matter whats after? Quote Link to comment https://forums.phpfreaks.com/topic/166864-solved-delete-images-with-cron/#findComment-879887 Share on other sites More sharing options...
dadamssg Posted July 21, 2009 Author Share Posted July 21, 2009 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?? Quote Link to comment https://forums.phpfreaks.com/topic/166864-solved-delete-images-with-cron/#findComment-879892 Share on other sites More sharing options...
webbnino Posted July 21, 2009 Share Posted July 21, 2009 That is good idea. go for it. Quote Link to comment https://forums.phpfreaks.com/topic/166864-solved-delete-images-with-cron/#findComment-879898 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.