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?
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.
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.
The modified date of a file or folder represents the last time that file or folder was updated.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With