Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keep last modified date when uploading file

Is there a way to preserve the last modified date when uploading a file via HTTP POST?

I already read that it gets changed when you use copy() (See here). But in my case, it's already changed in the temp folder.

HTML:

<!DOCTYPE html>
<html>
    <body>

        <form action="upload.php" method="post" enctype="multipart/form-data">
            Select file to upload:
            <input type="file" name="fileToUpload" id="fileToUpload">
            <input type="submit" value="Upload Image" name="submit">
        </form>

    </body>
</html>

PHP:

<?php

  $target_dir = "uploads/";
  $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
  $uploadOk = 1;
  $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);

  echo "Modified: ".date('d/m/Y H:i:s', filemtime($_FILES['fileToUpload']["tmp_name"]));

?>

The output is: Modified: 17/02/2016 09:02:39

But the file is actually last edited on 10/02/2016 09:34:23

Properties: (created, modified, access)

file properties

Is there a way to prevent that?

like image 646
Vince Avatar asked Feb 17 '16 08:02

Vince


People also ask

How do I stop the date modified from automatically changing?

The other way to prevent opening an Excel file from changing the folder's modified date is to ctrl-shift right click on file name and select open in protected mode.

Does date modified change when I copy a file?

In all examples, the modified date and time of a file does not change unless a property of the file has changed. The created date and time of the file changes depending on whether the file was copied or moved.

What does last modified mean on a file?

The modified date of a file or folder represents the last time that file or folder was updated.


1 Answers

The last modified date can be captured in the browser using the File.lastModified property. You can use JavaScript to set the value of a hidden input element to this date. Once the form is submitted, read the timestamp from the hidden input and use the method touch to set the timestamp on the newly created file on the server-side.

https://developer.mozilla.org/en-US/docs/Web/API/File/lastModified

https://www.php.net/touch

like image 58
Chad Skeeters Avatar answered Oct 12 '22 23:10

Chad Skeeters