简体   繁体   中英

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)

文件属性

Is there a way to prevent that?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM