Page 1 of 1

[Solved!] - Sending JPG modification time on upload ....

Posted: Tue Sep 27, 2005 11:33 pm
by sandej
First off, my assumption is that this is probably not possible, but I am going to ask anyway. ;)

In my gallery site I have created, I manually put in a date of when a picture was taken when I upload a picture to the site. When the source images are on Windows, their "Date Modified" field in Explorer is the actual date and time that I took the photo with my digital camera. I am seeking a way to convey that date automagically to my upload form, so that I know what date a picture was taken without having to look for it.

Through my testing, I already know that once a file is uploaded, it takes on a modification time of when the upload occured, and that original "Date Modified" field is completely lost. This throws out any use of filemtime() and the like. Is there anyway to get that date before I upload? If not with PHP, possibly some bit of javascript or somesuch?

Picture that I have selected which file I want to upload with the <input type=file> button, however I have not yet clicked on the final submit button. During this point, the browser knows what file I am going to choose, and in fact I use this info to show a preview of the local file in a second frame. (<IMG SRC=file:///blah/blah/blah.jpg>)

So anyway, give it some thought .... and then tell me it's impossible. :twisted:

Thanks in advance one way or the other!

Jim

Posted: Wed Sep 28, 2005 12:02 am
by feyd
not really possible.. however, if your camera stores exif metadata, the server can read that information. Often times, the timestamp (according to the camera) is part of this data.

Posted: Wed Sep 28, 2005 12:02 am
by ruchit
when uploading you can perhaps make use of some javascript which gets the file creation/modification date/time... & puts this value in a hidden field (on the picture upload form)... and you can store this value in the DB & fetch it when showing a particular image.

Posted: Wed Sep 28, 2005 12:04 am
by feyd
javascript is unable to make system level calls like that and the DOM does not support that kind of information.

Posted: Wed Sep 28, 2005 12:10 am
by ruchit
there's a built-in function (don't recall the name off the top) in javascript that does return datetime of file modification... he can use that function in onSubmit routine of the form & store that info in a hidden field... if i get the name of the function soon enough i'll post it

Posted: Wed Sep 28, 2005 12:14 am
by sandej
Hmm, I don't think my camera stores EXIF data, as it's several years old. That is a good thought, though. I will check into this.
when uploading you can perhaps make use of some javascript which gets the file creation/modification date/time... & puts this value in a hidden field (on the picture upload form)... and you can store this value in the DB & fetch it when showing a particular image.
This was my ray of hope after typing up my original post. This link seems to indicate it was at least possible at some point, though I admit that neither Firefox 1.5 nor IE 6.1 allowed his scripts to do what it looks like they should do. It did give me some hope though that perhaps I can add my own site to a trusted list that would allow it to work? I haven't had done anymore digging yet though as I just found it.

http://4umi.com/web/javascript/fileread.htm

Looks like fso.GetFile() is the possibility .....

Posted: Wed Sep 28, 2005 12:22 am
by sandej
if your camera stores exif metadata, the server can read that information. Often times, the timestamp (according to the camera) is part of this data.
Saaawwweeet! My images do have all kinds of EXIF metadata, very cool! :D And that data is maintained even on file uploads, just like you mentioned! Any pointers on a way to pull that with PHP (or another tool)?

I'm still interested in the javascript thing too, but at least it looks like this will definitely be a possibility now with or without it.

Thanks much!

Posted: Wed Sep 28, 2005 12:26 am
by feyd
unfortunately that's Jscript, not Javascript. Slightly different animal. XPCOM supports all sorts of system IO in Firefox, but for a page to use XPCOM, you have to disable a few parts of your security (not a good idea) .. Because of security issues, System IO with page scripting is extremely dangerous. You technically can use ActiveX and/or Java to do system IO, but it's not possible, nor ever likely will be through Javascript (by default)


but this is all fairly old news, as I've said it many times before (for the most part).. :?

Posted: Wed Sep 28, 2005 12:37 am
by sandej
Ah ok, makes sense on the JScript vs Javascript.

EXIF looks like the way to go for sure. At first glance, it looks like I can't use the built in PHP functions (exif_read_data() comes up undefined function when I call it from my web host) but I found this site which looks to have a very impressive toolkit for precicely this type of thing:

http://www.ozhiker.com/electronics/pjmt/index.html

EDIT:

Well I got this working by upgrading to PHP5 on my web host. I was then able to use exif_read_data() which is pretty self explanatory and ended up working really well! Basically now when I upload an image, I use that function to try and pull a date out of the exif. If there is a date, and it's not just 000-00-00 (the default if the camera doesn't know what date it is) then I pull that date and insert it into MySQL. If I can't snag a date automatically, I default back to whatever was chosen with the date selector tool (defaults to today).

Here is the very short, relevant piece of code:

Code: Select all

// get the EXIF date that the picture was taken
        // we will override any manual date entered if there
        // is a date in EXIF, otherwise we will take the manual date
        $tmp_file = $userfile['tmp_name'];

        $exif_arr = exif_read_data($tmp_file);
        $exif_date = $exif_arr['DateTimeOriginal'];

        // check to make sure there is EXIF data
        if (is_array($exif_arr) && $exif_date != '')
        {
            // the date is in the form of YYYY:MM:DD HH:MM:SS
            // split this into two chunks, then grab the year,mo,day
            $date_time = explode(' ', $exif_date);

            // only use the date if it's non-zero
            if ($date_time[0] != '0000:00:00')
            {
                list($year, $month, $day) = explode(":", $date_time[0]);
                $date = $year . '-' . $month . '-' . $day;
            }
        }
It all worked out really slick, thanks for the pointer to EXIF metadata!

I did quite a bit more searching and it does look like it would be pretty much impossible to use Javascript to pull a date client side before uploading. Either by getting the file modification time, or by reading the EXIF data. So it seems to only be something you can do on the server side, which makes sense. On the server side there are a variety of ways to read the EXIF stuff, including the built in PHP functions, that library I linked above which doesn't rely on the PHP functions, and command line tools such as JHead which could be called from your script.

Anyway, case closed and I will update the subject to reflect it. :)