PHP: how can I get file creation date?
Use filectime. For Windows it will return the creation time, and for Unix the change time which is the best you can get because on Unix there is no creation time (in most filesystems).
Note also that in some Unix texts the
ctime of a file is referred to as
being the creation time of the file.
This is wrong. There is no creation
time for Unix files in most Unix
filesystems.
How to know the creation time of a file by PHP?
filectime()
will do it but if you are using windows you can get creation time but if you are using UNIX you can get the change time and that would be all.
Getting the system date created of a file using PHP
SOLUTION:
Because you're uploading a file the date on the system is going to be the upload date. Your solution if it is an image file is to read the image file meta-data, which is a bit of a pain because there are multiple types of meta data for each type of image file and finding the one you want is not in itself efficient.
I think Imagick PHP plugin does this but I can't vouch for the quality or worth of this/these functions.
To clarify further:
When you upload a file from a computer to a server, the server will hold NO filesystem information about the file on the computer.
Meta Data is data about something, and typically data about a file (such as its created date) is not stored within the file itself as a standard, but in the operatng system that stores the file contents.
The server which recieves your uploaded file can not tell you when the file was saved on to the computer it came from, or anything else filesystem related because the server has only been given the file contents data, not the file systems metadata about storage and modification (etc).
If this information is available from within a file, it is stored in what is called "MetaData" inside the file itself, and to reach these bits of metadata you need to use something like Imagick
for images .
Alternatively, if you simply want the date the file was added to this system you can read that in this Stack Answer.
NOTES:
- You want
filectime
. - After you
header
add anexit
to cease execution. - You have a
$_POST
and a$_FILES
value with the same name, you are using$_POST['upload_file']
and$_FILES['upload_file']
I'm not sure if this will break your script but the POST values of this will be empty unless you have another field in your form with the same name which is clearly bad practise. - Your
require
and/orinclude
do not need to be in brackets. - It is also bad practise for
pathinfo
to be given a relative path, you should as much as possible give PHP functions absolute paths, using$_SERVER['DOCUMENT_ROOT']
or other magical constants. - Remove
basename
infilectime
, it's unneed. - it looks like
$path.$name
should infact be$path.$name.$ext
when usingmove_uploaded_file
Your problem (from your comment) is that you are looking for the time of a string that is not the file. Replace $_FILES["image_file"]["name"]
in you filectime
call to instead be ['tmp_name']
because this is the location address of where the uploaded file is (temporarily) stored.
The name
array value in $_FILES
simply tells you the name of the file from the place it was uploaded from.
BUT This time will simply only tell you the uploaded time.
- Your Error log should have shown you that your
filectime
is tying to get the data from a non-file entity.
Is there a way to read the actual file creation date with PHP?
When a user uploads a file in a web browser, what actually happens is that three pieces of information are added to the form submission data:
- A filename (which a browser will base on the name it had on the user's system)
- A file type (which will be the browser's best guess, usually just based on the filename)
- The binary contents of the file
Even if both the user's computer and your server have somewhere to record the creation date of the file, this won't be transmitted with the upload.
There's also something very important thing to bear in mind when building any web-based system: you only have the information the user gives you, and that information could be deliberately or accidentally wrong. If there was a timestamp, it might be wrong because the user's clock is wrong; or it might have been sent by a piece of software that let the user manually set it.
PHP: How to get creation date from uploaded file?
That data is not sent by the browser, so there's no way to access it. The data sent along with the file is mime-type
, filename
and file contents.
If you want the creation date, you'll either need the user to provide it or create a special file uploading mechanism via Flash or Java.
Get file creation date for filename for each file rather than for 1 file only?
Replace the whole foreach
loop with this.
for ($i = 1; $i <= 12; $i++) {
if (file_exists($items[$i])) {
echo "</tr>";
echo "<td><font face='Arial' size='3'>$i</font></td>";
echo "<td><font face='Arial' size='3' color='red'>" . date("F d Y H:i", filemtime($items[$i]));
echo "</font></td>";
}
print("
<td><img src='$dirs[0]/$cam1[$i]' height='80' width='80'></td>
<td><img src='$dirs[1]/$cam2[$i]' height='80' width='80'></td>
<td><img src='$dirs[2]/$cam3[$i]' height='80' width='80'></td>
");
if ($i === 12) break; //unnecessary should/could be removed
}
Replaced $filename
with $items[$i]
How do I get how long the file was created in seconds in PHP
From: https://www.php.net/manual/en/function.filectime.php
You can use the filectime
function to get the creation date of your file. The function returns a unix timestamp. You could then subtract that time from the current time to get the amount of seconds ago the file was made.
<?php
# Get the creation date of the file in unix timestamp
$creationDate = filectime("myfile.txt");
# Subtract the creation date from the current time, to get the time difference in seconds
$secondsAgo = time() - $creationDate;
?>
Please note that on most unix systems it will return the last modified date instead of the creation date. This is because on most unix systems the creation date does not exist like on windows.
Related Topics
PHP Curl, Extract an Xml Response
Decode Gzipped Web Page Retrieved Via Curl in PHP
Passing Multiple Variables to Another Page in Url
Why Doesn't MySQL Support Millisecond/Microsecond Precision
PHP, Getting Variable from Another PHP-File
Prevent Sent Emails Treated as Junk Mails Using PHP Mail Function
How to Linkify Urls in a String With PHP
How to Strip a Tag and All of Its Inner HTML Using the Tag'S Id
Calculating Days of Week Given a Week Number
How to Use a String as an Array Index Path to Retrieve a Value
Get the Date of Next Monday, Tuesday, etc
If Statement in the Middle of Concatenation
PHP Check If File Contains a String
Difference Between Session_Unset() and Session_Destroy() in PHP