HaLo2FrEeEk Posted August 3, 2008 Share Posted August 3, 2008 I have a script set up that will transfer a picture from one server to my own every 10 minutes (using cron). The script uses an external cURL based class to retrieve the file on systems that don't have file_get_contents turned on. What I want is for only 24 hours worth of pictures to be on the server at any one time (give or take 1, I'm not too picky). The files are saved with the filename as a timestamp, so there is no overwriting, but it's irregular (since the script runs every 10 minutes, but it could take longer for the remote server to respond, thus making the timestamps not exactly 10 minutes apart, it's usually a difference of 3 or 4 seconds either way at the most, though). How would I go about deleting the 24 hour old file after a new one is written? I suppose I could make a rather complicated script that would name the file based on what time of day it is (what 10-minute chunk it is during the day), but that would require a lot of math, and I'd prefer a simpler method. Can anyone give any pointers on how I'd do this, I've been racking my brain trying to figure something simple out, but I can't think of anything. Quote Link to comment https://forums.phpfreaks.com/topic/117923-solved-deleting-a-file-after-24-hours/ Share on other sites More sharing options...
Xurion Posted August 3, 2008 Share Posted August 3, 2008 Do you have access to a database? I'm thinking that you should has a table that keeps track of the images in a table, storing the image path and the time it was uploaded. By comparing the date stored and the current date it should be fairly simple to delete the files that are more than 24 hours old. Quote Link to comment https://forums.phpfreaks.com/topic/117923-solved-deleting-a-file-after-24-hours/#findComment-606581 Share on other sites More sharing options...
LemonInflux Posted August 3, 2008 Share Posted August 3, 2008 Have a timestamp and use DELETE FROM `table` WHERE `timestamp field` = (NOW() - INTERVAL 1 DAY) I think so, anyway. Not sure. ---------------- Now playing: August Burns Red - The Truth of a Liar via FoxyTunes Quote Link to comment https://forums.phpfreaks.com/topic/117923-solved-deleting-a-file-after-24-hours/#findComment-606606 Share on other sites More sharing options...
ShaunO Posted August 3, 2008 Share Posted August 3, 2008 If you don't have access to a database.. I would scan the files that are in the directory to be deleted Loop through each file and use filemtime(); on them and if it is older than 24 hours, unlink(); it I would probably be more inclined to runit every hour or so if there are many files to check Best bet would be a database though for sure. Quote Link to comment https://forums.phpfreaks.com/topic/117923-solved-deleting-a-file-after-24-hours/#findComment-606627 Share on other sites More sharing options...
HaLo2FrEeEk Posted August 4, 2008 Author Share Posted August 4, 2008 I do have a database, yes, but I was looking for something really simple. I was afraid I'd have to scan the files, though. Since I transfer the picture every 10 minutes there are 6 per hour, or 144 per day, that would take a long time to scan, most likely. The problem is, even with using a database, there's no way to regulate the timestamps. Like I said, one image might transfer faster than another, meaning the timestamp can be a few seconds off either way. If there is a way to regulate the timestamp so that the difference between each is exactly 600 seconds (so a file transfered at 11:10 pm on 08/03/08 would be 1217830200, +600 would be 1217830800, +600 would be 1217831400; so that's 11:10pm, 11:20pm, and 11:30pm). Is there a way to do this? Could I get the time of day using some fancy math, then round down to the 10th minute and run strtotime()? I can't really figure out how I'd do that. It might actually be more convenient to use a database because I plan on making a gallery to display these images up-to-date, and use an external class to generate a dynamic animated gif using the images in the folder. Anyways, what, in your opinions, would be easiest? Using a database, or a scanning the folder? Quote Link to comment https://forums.phpfreaks.com/topic/117923-solved-deleting-a-file-after-24-hours/#findComment-607253 Share on other sites More sharing options...
HaLo2FrEeEk Posted August 6, 2008 Author Share Posted August 6, 2008 bump. Quote Link to comment https://forums.phpfreaks.com/topic/117923-solved-deleting-a-file-after-24-hours/#findComment-610097 Share on other sites More sharing options...
trq Posted August 6, 2008 Share Posted August 6, 2008 The easiest way would be to add this to your crontab.... 0 * * * * /usr/bin/find /path/to/images -mtime +24 -exec rm {} \; This will run once an hour on the hour checking the /path/to/images directory, any files within this directory older than 24hrs will be removed. Quote Link to comment https://forums.phpfreaks.com/topic/117923-solved-deleting-a-file-after-24-hours/#findComment-610185 Share on other sites More sharing options...
Naez Posted August 6, 2008 Share Posted August 6, 2008 Yep was just about to suggest a cron job. Quote Link to comment https://forums.phpfreaks.com/topic/117923-solved-deleting-a-file-after-24-hours/#findComment-610189 Share on other sites More sharing options...
HaLo2FrEeEk Posted August 7, 2008 Author Share Posted August 7, 2008 I didn't know such a simple code existed. As it is I have my image transfer run once every 10 minutes, so I'll probably run that, then run the code you provided. I like it. Quote Link to comment https://forums.phpfreaks.com/topic/117923-solved-deleting-a-file-after-24-hours/#findComment-610465 Share on other sites More sharing options...
HaLo2FrEeEk Posted August 8, 2008 Author Share Posted August 8, 2008 Ok, I put in the code you put up, for the cron job, this is the code I'm using: 0 * * * * /usr/bin/find /home/halo2freeek/infectionist.com/misc/bungie_webcam/images -mtime +24 -exec rm {} \; That's the path to my images, but what's with the 0 and the * * * at the beginning, and the usr/bin/find? Is that something I have to change, what do I need to point it to? I have it set up to email me the output everytime it's run and this is the email I got: sh: line 1: 0: command not found What's that mean? Quote Link to comment https://forums.phpfreaks.com/topic/117923-solved-deleting-a-file-after-24-hours/#findComment-611460 Share on other sites More sharing options...
mbeals Posted August 8, 2008 Share Posted August 8, 2008 the 0 * * * * is the cron schedule.... run on minute 0, every hour, every day of the month, every month of the year, every day of the week. man crontab for more details. I'm guessing you aren't installing the cronjob correctly. from shell, type in: crontab -e <enter> This opens the crontab. Paste that text in there and exit (with save). /usr/bin/find is the path to the find binary which is searching the folder for the files and removing them. If cron still isn't working, just paste this directly to a command line: /usr/bin/find /home/halo2freeek/infectionist.com/misc/bungie_webcam/images -mtime +24 -exec rm {} if you still get an error, post it. Quote Link to comment https://forums.phpfreaks.com/topic/117923-solved-deleting-a-file-after-24-hours/#findComment-611539 Share on other sites More sharing options...
HaLo2FrEeEk Posted August 9, 2008 Author Share Posted August 9, 2008 I don't have to use shell for crontab, my host provides a nice little interface to add new cronjobs when needed. They won't let me put in scheduling information since I set that with the interface seperately. I have it set up to email me the output though, so I removed the schedule info and put this: /usr/bin/find /home/halo2freeek/infectionist.com/misc/bungie_webcam/images -mtime +24 -exec rm {} And I haven't gotten an email. I set it to run on the 42nd minute, since it was 1:40 when I tried it and I wanted to see the result quickly. I will set it to do it every 10 minutes and see if it makes a difference, but it won't. I'm thinking t might just not email me the result if there is nothing to delete. I changed my server around a little and changed my domain's root folder name, and I didn't change it in the cronjob, I fixed it last night though and it's copying the pictures again, so hopefully in a few hours I'll get an email telling me it was successful. Quote Link to comment https://forums.phpfreaks.com/topic/117923-solved-deleting-a-file-after-24-hours/#findComment-612129 Share on other sites More sharing options...
HaLo2FrEeEk Posted August 12, 2008 Author Share Posted August 12, 2008 Sorry for the double post, but I can't get the cron to work. I put it in there, it's active, and it's scheduled to run every 10 minutes, but for some reason, it's just not deleting the files. Is there somthing I have to do to set the filetime on the images or is it automatic? This is the cron code I have in there now: /usr/bin/find /home/halo2freeek/infectionist.com/misc/bungie_webcam/images -mtime +24 -exec rm {} \; And it's not deleting the file. I don't want to have hundreds of images on the server, there are only supposed to be 144 +/- 1 at any given time. Quote Link to comment https://forums.phpfreaks.com/topic/117923-solved-deleting-a-file-after-24-hours/#findComment-614272 Share on other sites More sharing options...
HaLo2FrEeEk Posted August 13, 2008 Author Share Posted August 13, 2008 Ok, I pasted the command above into the shell command line (I'm using putty), and this is what got: /usr/bin/find: missing argument to `-exec' What's that? Quote Link to comment https://forums.phpfreaks.com/topic/117923-solved-deleting-a-file-after-24-hours/#findComment-615159 Share on other sites More sharing options...
trq Posted August 13, 2008 Share Posted August 13, 2008 No idea. The command works perfectly here. Quote Link to comment https://forums.phpfreaks.com/topic/117923-solved-deleting-a-file-after-24-hours/#findComment-615162 Share on other sites More sharing options...
HaLo2FrEeEk Posted August 13, 2008 Author Share Posted August 13, 2008 Should I write to my host's tech support and ask them what the problem is? It might be a setting on their side. I'll do that and post the results here. Quote Link to comment https://forums.phpfreaks.com/topic/117923-solved-deleting-a-file-after-24-hours/#findComment-615247 Share on other sites More sharing options...
trq Posted August 13, 2008 Share Posted August 13, 2008 Rediculous question but, is rm in your path? Try using /bin/rm instead. Quote Link to comment https://forums.phpfreaks.com/topic/117923-solved-deleting-a-file-after-24-hours/#findComment-615255 Share on other sites More sharing options...
HaLo2FrEeEk Posted August 13, 2008 Author Share Posted August 13, 2008 So I'd use: /usr/bin/find /home/halo2freeek/infectionist.com/misc/bungie_webcam/images -mtime +24 -exec bin/rm {} \; or /usr/bin/rm /home/halo2freeek/infectionist.com/misc/bungie_webcam/images -mtime +24 -exec rm {} \; Which one? No question is rediculous for someone in my position, I just want to get this resolved quickly. Quote Link to comment https://forums.phpfreaks.com/topic/117923-solved-deleting-a-file-after-24-hours/#findComment-615299 Share on other sites More sharing options...
trq Posted August 13, 2008 Share Posted August 13, 2008 /usr/bin/find /home/halo2freeek/infectionist.com/misc/bungie_webcam/images -mtime +24 -exec /bin/rm {} \; Quote Link to comment https://forums.phpfreaks.com/topic/117923-solved-deleting-a-file-after-24-hours/#findComment-615481 Share on other sites More sharing options...
HaLo2FrEeEk Posted August 14, 2008 Author Share Posted August 14, 2008 Ok, I ran exactly that command from the root folder under putty (the root folder is home/halo2freeek, but I think it shouldn't matter where I run it from because it has the path to the images). It didn't return an error, but it didn't remove any files. What I learned is that +24 will remove files more than 24 days old, if I want to remove files 0 days or older (meaning anywhere from 0 - 86,399 seconds old), I need to change +24 to +0 and it works like a charm. I ran that command and it removed all the files leaving 144 files after. Thank you everyone for your help, I really appreciate it! Quote Link to comment https://forums.phpfreaks.com/topic/117923-solved-deleting-a-file-after-24-hours/#findComment-616184 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.