0

I have no idea why filetime is giving me the wrong date. Anyone have any suggestions?

for($i=0;$i<$image_count;$i++){                         //Setup file names and file types
    $temp_name = $target_dir . basename($_FILES["fileToUpload"]["name"][$i]);
    $temp_thumb_name = $thumb_target_dir . basename($_FILES["fileToUpload"]["name"][$i]);
    $temp_type = pathinfo($temp_name,PATHINFO_EXTENSION);
    array_push($target_file, $temp_name);               // Create array of file names
    array_push($target_file_thumb, $temp_thumb_name);   //Create array of thumb path names
    array_push($imageFileType, $temp_type);             //Create array of fileextensions
    echo "<BR>was last modified: " . date ("F d Y H:i:s.", filemtime(basename($_FILES["fileToUpload"]["name"][$i])));
    array_push($file_creation, date ("F d Y H:i:s.", filemtime(basename($_FILES["fileToUpload"]["name"][$i]))));
}

Output

was last modified: January 01 1970 01:00:00.

Error log

PHP Warning:  filemtime(): stat failed for DSC07292.jpg
David Jack
  • 87
  • 6
  • remove `basename` when using `filetime` you need the full path of the file – cmorrissey Aug 04 '17 at 16:19
  • Where do you upload the files to? Path? In the code you write target_dir. Basename gives you without path. It seems you upload to a directory but try to look at filemtime in basename (parent folder). In short, try: `date ("F d Y H:i:s.", filemtime($target_dir . basename ($_FILES["fileToUpload"]["name"][$i])));` – Andreas Aug 04 '17 at 16:21
  • I am trying to get the file creation dates of the original files the user uploads rather than the files I create on my server. I have also tried this code without the basename and received the same output and error. Thanks for taking the time to look. – David Jack Aug 04 '17 at 16:23
  • See my updated comment above – Andreas Aug 04 '17 at 16:24
  • @Andreas. Still same issue. Thanks for the suggestion – David Jack Aug 04 '17 at 16:33
  • Var_dump your array. And what is the folder of the file you are trying to get info on. And post it here – Andreas Aug 04 '17 at 16:40
  • Once you've fixed the path issue, i bet you'll be disapointed : file creation date is a meta data of a file, it's managed by the file system (if it wants to) and as such is not **inside** the file. This piece of information is not transferred during file upload. Unless you have files in which there may be meta information (like exif in images), there's no way to get the creation date of original files. By the way, you want Creation date but are using a function called fileMtime, the 'M' stands for Modification time... – Zimmi Aug 04 '17 at 17:09

3 Answers3

0

I think the problem is in the path try this :

array_push($file_creation, date ("F d Y H:i:s.", filemtime($temp_name)));
  • Thanks for the suggestion. Still the same issues. Error log now says PHP Warning: filemtime(): stat failed for uploads/1/DSC07346.jpg in /Applications/MAMP/htdocs/upload.php on line 55 But date is still the wrong value "January 01 1970 01:00:00." – David Jack Aug 04 '17 at 16:31
  • try to provide a full path of your file to the function filemtime – Abderrahim Soubai-Elidrisi Aug 04 '17 at 16:46
0

The reason you get the output 1970 at one o'clock is because you are in a timezone (now) that is 1 hour+ compared with GMT.
What happens is that filemtime doesn't find the file or something else is wrong, but most likely a path error.
Filemtime returns false.

Date reads the false as 0. Date looks at the date in UNIX time 0 which is midnight 1970.

Your timezone adds 1 hour.

Output is 1 hour past midnight.

I can bet my left arm on it's a path error or that the file doesn't exist that you try filemtime on.

Make sure your array is correct, and that the file is really stored in basename() or webroot. (Highly doubtful about that).
If it's not in webroot you need to add the path in the filemtime () like:.

date ("F d Y H:i:s.", filemtime("THE PATH" . basename ($_FILES["fileToUpload"]["name"][$i])));

Also the path may need a /.

Edit:
I see now that you gave the path in comments to the other answer.

Try this:

date ("F d Y H:i:s.", filemtime($_SERVER["DOCUMENT_ROOT"]  . "/uploads/1/" . basename ($_FILES["fileToUpload"]["name"][$i])));
Andreas
  • 23,610
  • 6
  • 30
  • 62
0

Have a read for the docs for file upload : how to handle file uploads

  1. The uploaded file is located at $_FILES["fileToUpload"]["tmp_name"][$i] (Read it carefully, it is tmp_name)
  2. The name of the file in $_FILES["fileToUpload"]["name"][$i] does not contain any path. Useless to basename it.
  3. You need to move the uploaded file to the location you want with the function move_uploaded_file

Once you've fixed the path issue, i bet you'll be disapointed : file creation date is a meta data of a file, it's managed by the file system (if it wants to) and as such is not inside the file.

This piece of information is not transferred during file upload. Unless you have files in which there may be meta information (like exif in images), there's no way to get the creation date of original files. By the way, you want Creation date but are using a function called fileMtime, the 'M' stands for Modification time...

For the rest Andreas explained you why date is 1970, as filemtime does not find a file to stat

// __DIR__ is directory of this php file, set accordingly
$target_dir = __DIR__ . '/';
$thumb_target_dir = __DIR__ . '/';

$image_count = count($_FILES["fileToUpload"]["name"]);
for ($i = 0; $i < $image_count; $i++) {
    //Setup file names and file types

    // Create the path to the location where we want to store the file
    $temp_name = $target_dir . $_FILES["fileToUpload"]["name"][$i];  

    // We need to move the temp file to the location we want :
    move_uploaded_file($_FILES["fileToUpload"]["tmp_name"][$i], $temp_name);

    $temp_thumb_name = $thumb_target_dir . $_FILES["fileToUpload"]["name"][$i];
    $temp_type = pathinfo($temp_name, PATHINFO_EXTENSION);
    array_push($target_file, $temp_name);               // Create array of file names
    array_push($target_file_thumb, $temp_thumb_name);   //Create array of thumb path names
    array_push($imageFileType, $temp_type);             //Create array of fileextensions

    // Get the file modif time, but only of the local file.
    $fmtime = filemtime($temp_name);
    echo "<BR>was last modified: " . date("F d Y H:i:s.", $fmtime);
    array_push($file_creation, date("F d Y H:i:s.", $fmtime));
}
Zimmi
  • 1,599
  • 1
  • 10
  • 14
  • Excellent. Thank you for the detailed response. It is images and I have used exif to get the orientation. I'll spend more time looking at that. – David Jack Aug 04 '17 at 18:41
  • Fixed using the following 2 lines '$exif = exif_read_data($_FILES["fileToUpload"]["tmp_name"][$i]); //reads meta data array_push($file_creation, $exif['DateTimeOriginal']);' – David Jack Aug 05 '17 at 10:28