[PHP] Checking file existence
I have files that are named in the following manner: file..jpg (where is an hhmm timestamp [hour and minutes]) What's the best way to take the current hhmm timestamp and run backwards finding the previous 5 files, and checking whether they actually exist? There's a possibility one or more might not exist and rather than returning a blank value, I want to continue further back till I find the next. So for example, say I have: file 1122.jpg file 1121.jpg file 1119.jpg file 1117.jpg file 1116.jpg file 1114.jpg Notice the gaps, 1120, 1118, and 1115 are missing. But I still want to return five, thus 1122, 1121, 1119, 1117, and 1116. I thought of taking the current timestamp and loop five times, subtracting from it every time, but that's assuming all five are there. If one's missing then I only have four images. -- W | It's not a bug - it's an undocumented feature. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Imaging . 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Checking file existence
Ashley M. Kirchner wrote: I have files that are named in the following manner: file..jpg (where is an hhmm timestamp [hour and minutes]) What's the best way to take the current hhmm timestamp and run backwards finding the previous 5 files, and checking whether they actually exist? There's a possibility one or more might not exist and rather than returning a blank value, I want to continue further back till I find the next. So for example, say I have: file 1122.jpg file 1121.jpg file 1119.jpg file 1117.jpg file 1116.jpg file 1114.jpg Notice the gaps, 1120, 1118, and 1115 are missing. But I still want to return five, thus 1122, 1121, 1119, 1117, and 1116. I thought of taking the current timestamp and loop five times, subtracting from it every time, but that's assuming all five are there. If one's missing then I only have four images. -- W | It's not a bug - it's an undocumented feature. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Imaging . 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- View this message in context: http://www.nabble.com/Checking-file-existence-tf2636778.html#a7359768 Sent from the PHP - General mailing list archive at Nabble.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Checking file existence
sorry, clicked on send too fast ;) i'd extract the minues and seconds , generate an array of timestamps and sort that array desc -- View this message in context: http://www.nabble.com/Checking-file-existence-tf2636778.html#a7359891 Sent from the PHP - General mailing list archive at Nabble.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Checking file existence
one solution could probably be made using: glob(), sort() and/or filemtime(). btw: I assume your aware that the filenames are not very unique (i.e. twice a day, everyday a given file will potentially be overwritten) and that this is intentional Ashley M. Kirchner wrote: I have files that are named in the following manner: file..jpg (where is an hhmm timestamp [hour and minutes]) What's the best way to take the current hhmm timestamp and run backwards finding the previous 5 files, and checking whether they actually exist? There's a possibility one or more might not exist and rather than returning a blank value, I want to continue further back till I find the next. So for example, say I have: file 1122.jpg file 1121.jpg file 1119.jpg file 1117.jpg file 1116.jpg file 1114.jpg Notice the gaps, 1120, 1118, and 1115 are missing. But I still want to return five, thus 1122, 1121, 1119, 1117, and 1116. I thought of taking the current timestamp and loop five times, subtracting from it every time, but that's assuming all five are there. If one's missing then I only have four images. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Checking file existence
Edward Kay wrote: Are all the files called file..jpg or could they have other names such as clouds.1123.jpg, field.2034.jpg etc? They are all the same exact file name with the only difference being the timestamp. Alternatively, you could get the OS to do the work through an exec call to something like 'ls *..jpg' or similar. The downside of this however is that it is then OS dependent. I thought of that too, but only because of the way the files are stored. I completely forgot about one other catch... I thought about it but my hands were too quick and I didn't type that paragraph in. The files are stored as follows on the file system: /.year/month/day/hour/file.hhmm.jpg (yes that dot preceding year is correct) So I can look right now and find /.2006/11/15/08/file.0846.jpg Each /hour/ folder contains at most 60 files so in essence doing either a scandir() or an exec call to ls wouldn't take that long. However, I run into problems when I need to traverse to the previous hour to find images. Say I load up a file with timestamp 0802. I now need to find 0801, 0800, 0759, 0758, and 0757. The last three being in the previous folder. Unless I run scandir() again the entire /day/ folder and sort, I'd have to figure out how to go on about that. Like I said, I completely forgot to write this piece of the puzzle in my previous e-mail. I blame it on the lack of caffeine. -- A -- W | It's not a bug - it's an undocumented feature. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Imaging . 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Checking file existence
UntestedCode $img_array = array(); $i = 0; while(count($img_array) 5) { $hhmm = date(Hi, strtotime(-$i minutes)); $filename = file.$hhmm.jpg; if(file_exists(/path/to/$filename)) { $img_array[] = $filename; } $i++; } print_r($img_array); /UntestedCode -Original Message- From: Ashley M. Kirchner [mailto:[EMAIL PROTECTED] Sent: Wednesday, November 15, 2006 10:19 AM To: PHP General List Subject: [PHP] Checking file existence I have files that are named in the following manner: file..jpg (where is an hhmm timestamp [hour and minutes]) What's the best way to take the current hhmm timestamp and run backwards finding the previous 5 files, and checking whether they actually exist? There's a possibility one or more might not exist and rather than returning a blank value, I want to continue further back till I find the next. So for example, say I have: file 1122.jpg file 1121.jpg file 1119.jpg file 1117.jpg file 1116.jpg file 1114.jpg Notice the gaps, 1120, 1118, and 1115 are missing. But I still want to return five, thus 1122, 1121, 1119, 1117, and 1116. I thought of taking the current timestamp and loop five times, subtracting from it every time, but that's assuming all five are there. If one's missing then I only have four images. -- W | It's not a bug - it's an undocumented feature. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Imaging . 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Checking file existence
MoreUntestedCode $img_array = array(); $i = 0; while(count($img_array) 5) { $year = date(Y, strtotime(-$i minutes)); $month= date(m, strtotime(-$i minutes)); $day = date(d, strtotime(-$i minutes)); $hour = date(H, strtotime(-$i minutes)); $hhmm = date(Hi, strtotime(-$i minutes)); $filename = file.$hhmm.jpg; if(file_exists(/path/to/$year/$month/$day/$hour/$filename)) { $img_array[] = $filename; } $i++; } print_r($img_array); /MoreUntestedCode -Original Message- From: Brad Fuller [mailto:[EMAIL PROTECTED] Sent: Wednesday, November 15, 2006 10:56 AM To: php-general@lists.php.net Subject: RE: [PHP] Checking file existence UntestedCode $img_array = array(); $i = 0; while(count($img_array) 5) { $hhmm = date(Hi, strtotime(-$i minutes)); $filename = file.$hhmm.jpg; if(file_exists(/path/to/$filename)) { $img_array[] = $filename; } $i++; } print_r($img_array); /UntestedCode -Original Message- From: Ashley M. Kirchner [mailto:[EMAIL PROTECTED] Sent: Wednesday, November 15, 2006 10:19 AM To: PHP General List Subject: [PHP] Checking file existence I have files that are named in the following manner: file..jpg (where is an hhmm timestamp [hour and minutes]) What's the best way to take the current hhmm timestamp and run backwards finding the previous 5 files, and checking whether they actually exist? There's a possibility one or more might not exist and rather than returning a blank value, I want to continue further back till I find the next. So for example, say I have: file 1122.jpg file 1121.jpg file 1119.jpg file 1117.jpg file 1116.jpg file 1114.jpg Notice the gaps, 1120, 1118, and 1115 are missing. But I still want to return five, thus 1122, 1121, 1119, 1117, and 1116. I thought of taking the current timestamp and loop five times, subtracting from it every time, but that's assuming all five are there. If one's missing then I only have four images. -- W | It's not a bug - it's an undocumented feature. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Imaging . 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Checking file existence
Hi Ashley, Are all the files called file..jpg or could they have other names such as clouds.1123.jpg, field.2034.jpg etc? If they all have the same name, you could use something like scandir() and then just use the first 5 items in the returned array. You could also loop through the directory with readdir() and add the filenames to an array. You could index this array with the 4 digit string, ksort it and then use the first/last 5 entries. This would work even if the files had different names. The downside of these suggestions is that if there are lots of files you would end up with a large array stored in memory. Alternatively, you could get the OS to do the work through an exec call to something like 'ls *..jpg' or similar. The downside of this however is that it is then OS dependent. Hope this gives you some ideas though. Edward -Original Message- From: Ashley M. Kirchner [mailto:[EMAIL PROTECTED] Sent: 15 November 2006 15:19 To: PHP General List Subject: [PHP] Checking file existence I have files that are named in the following manner: file..jpg (where is an hhmm timestamp [hour and minutes]) What's the best way to take the current hhmm timestamp and run backwards finding the previous 5 files, and checking whether they actually exist? There's a possibility one or more might not exist and rather than returning a blank value, I want to continue further back till I find the next. So for example, say I have: file 1122.jpg file 1121.jpg file 1119.jpg file 1117.jpg file 1116.jpg file 1114.jpg Notice the gaps, 1120, 1118, and 1115 are missing. But I still want to return five, thus 1122, 1121, 1119, 1117, and 1116. I thought of taking the current timestamp and loop five times, subtracting from it every time, but that's assuming all five are there. If one's missing then I only have four images. -- W | It's not a bug - it's an undocumented feature. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Imaging . 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Checking file existence
Surely it would be simpler to store all this info in a database table? e.g. id : int created : timestamp filename : varchar Only files that actually existed would be in the DB, plus you could do all your sorting on the 'created' timestamp column. Querying the DB should be much simpler and more efficient than using the filesystem. Aside from anything else, it would make your program a lot easier to understand to anyone having to maintain it in the future. Edward Edward Kay wrote: Are all the files called file..jpg or could they have other names such as clouds.1123.jpg, field.2034.jpg etc? They are all the same exact file name with the only difference being the timestamp. Alternatively, you could get the OS to do the work through an exec call to something like 'ls *..jpg' or similar. The downside of this however is that it is then OS dependent. I thought of that too, but only because of the way the files are stored. I completely forgot about one other catch... I thought about it but my hands were too quick and I didn't type that paragraph in. The files are stored as follows on the file system: /.year/month/day/hour/file.hhmm.jpg (yes that dot preceding year is correct) So I can look right now and find /.2006/11/15/08/file.0846.jpg Each /hour/ folder contains at most 60 files so in essence doing either a scandir() or an exec call to ls wouldn't take that long. However, I run into problems when I need to traverse to the previous hour to find images. Say I load up a file with timestamp 0802. I now need to find 0801, 0800, 0759, 0758, and 0757. The last three being in the previous folder. Unless I run scandir() again the entire /day/ folder and sort, I'd have to figure out how to go on about that. Like I said, I completely forgot to write this piece of the puzzle in my previous e-mail. I blame it on the lack of caffeine. -- A -- W | It's not a bug - it's an undocumented feature. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Imaging . 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Checking file existence
Very nifty Brad, One addition though, so she/you can work back from any point: MoreUntestedCode $i = 0; $start = strtotime(2006-10-05 12:05am); while(count($img_array) 5) { $year = date(Y, strtotime(-$i minutes, $start)); $month= date(m, strtotime(-$i minutes, $start)); $day = date(d, strtotime(-$i minutes, $start)); $hour = date(H, strtotime(-$i minutes, $start)); $hhmm = date(Hi, strtotime(-$i minutes, $start)); $filename = file.$hhmm.jpg; if(file_exists(/path/to/$year/$month/$day/$hour/$filename)) { $img_array[] = $filename; } $i++; } print_r($img_array); /MoreUntestedCode JP -Original Message- From: Brad Fuller [mailto:[EMAIL PROTECTED] Sent: Wednesday, November 15, 2006 10:11 AM To: php-general@lists.php.net Subject: RE: [PHP] Checking file existence MoreUntestedCode $img_array = array(); $i = 0; while(count($img_array) 5) { $year = date(Y, strtotime(-$i minutes)); $month= date(m, strtotime(-$i minutes)); $day = date(d, strtotime(-$i minutes)); $hour = date(H, strtotime(-$i minutes)); $hhmm = date(Hi, strtotime(-$i minutes)); $filename = file.$hhmm.jpg; if(file_exists(/path/to/$year/$month/$day/$hour/$filename)) { $img_array[] = $filename; } $i++; } print_r($img_array); /MoreUntestedCode -Original Message- From: Brad Fuller [mailto:[EMAIL PROTECTED] Sent: Wednesday, November 15, 2006 10:56 AM To: php-general@lists.php.net Subject: RE: [PHP] Checking file existence UntestedCode $img_array = array(); $i = 0; while(count($img_array) 5) { $hhmm = date(Hi, strtotime(-$i minutes)); $filename = file.$hhmm.jpg; if(file_exists(/path/to/$filename)) { $img_array[] = $filename; } $i++; } print_r($img_array); /UntestedCode -Original Message- From: Ashley M. Kirchner [mailto:[EMAIL PROTECTED] Sent: Wednesday, November 15, 2006 10:19 AM To: PHP General List Subject: [PHP] Checking file existence I have files that are named in the following manner: file..jpg (where is an hhmm timestamp [hour and minutes]) What's the best way to take the current hhmm timestamp and run backwards finding the previous 5 files, and checking whether they actually exist? There's a possibility one or more might not exist and rather than returning a blank value, I want to continue further back till I find the next. So for example, say I have: file 1122.jpg file 1121.jpg file 1119.jpg file 1117.jpg file 1116.jpg file 1114.jpg Notice the gaps, 1120, 1118, and 1115 are missing. But I still want to return five, thus 1122, 1121, 1119, 1117, and 1116. I thought of taking the current timestamp and loop five times, subtracting from it every time, but that's assuming all five are there. If one's missing then I only have four images. -- W | It's not a bug - it's an undocumented feature. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Imaging . 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Checking file existence
Stut wrote: mall, minor, insignificant point If I have less than 5 files this loop will never end. But you all knew that right? Yup. I break out of the loop if $i reaches 10 for some weird reason. Also, depending on the frequency of files (i.e. are there months-worth of gaps or minutes at most), the following might be faster... Theoretically, minutes. I mean, the thing has been down in the past, for months at a time, but it's been running fine for the past month (and I plan on keeping it that way.) So, unless I get hit by a bus, and unless the file system gets full, and unless somehow the camera quits, it'll be fine. I know, way too many 'unless's... It's a way of life. :) -- A -- W | It's not a bug - it's an undocumented feature. + Ashley M. Kirchner mailto:[EMAIL PROTECTED] . 303.442.6410 x130 IT Director / SysAdmin / Websmith . 800.441.3873 x130 Photo Craft Imaging . 3550 Arapahoe Ave. #6 http://www.pcraft.com . . .. Boulder, CO 80303, U.S.A. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Checking file existence
At 8:18 AM -0700 11/15/06, Ashley M. Kirchner wrote: I have files that are named in the following manner: file..jpg (where is an hhmm timestamp [hour and minutes]) What's the best way to take the current hhmm timestamp and run backwards finding the previous 5 files, and checking whether they actually exist? There's a possibility one or more might not exist and rather than returning a blank value, I want to continue further back till I find the next. So for example, say I have: file 1122.jpg file 1121.jpg file 1119.jpg file 1117.jpg file 1116.jpg file 1114.jpg My suggestion is to read all the files in the directory into an array, sort the array, and pull out the top five. That way it makes no difference if there are any gaps and you also check to see if the files do exist. Also, at some point, you'll need to delete, and that's not a bad way to delete the older files either. tedd -- --- http://sperling.com http://ancientstones.com http://earthstones.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Checking file existence
John Pillion wrote: Very nifty Brad, One addition though, so she/you can work back from any point: MoreUntestedCode $i = 0; $start = strtotime(2006-10-05 12:05am); while(count($img_array) 5) { $year = date(Y, strtotime(-$i minutes, $start)); $month= date(m, strtotime(-$i minutes, $start)); $day = date(d, strtotime(-$i minutes, $start)); $hour = date(H, strtotime(-$i minutes, $start)); $hhmm = date(Hi, strtotime(-$i minutes, $start)); $filename = file.$hhmm.jpg; if(file_exists(/path/to/$year/$month/$day/$hour/$filename)) { $img_array[] = $filename; } $i++; } print_r($img_array); /MoreUntestedCode Small, minor, insignificant point If I have less than 5 files this loop will never end. But you all knew that right? Also, depending on the frequency of files (i.e. are there months-worth of gaps or minutes at most), the following might be faster... * glob the files in the first dir * binary chop to find the nearest to your start time * go back in the array from that point to find your 5 * if you hit the start of the array, go down a day, repeat the glob and start from the end of the new array -Stut -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php