Vince
Vince

Reputation: 1133

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?

Upvotes: 6

Views: 5238

Answers (2)

Chad Skeeters
Chad Skeeters

Reputation: 1488

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

Upvotes: 2

defro
defro

Reputation: 56

Sorry you cannot keep the file information. The uploaded file is considered as new file.

Upvotes: 1

Related Questions