Php date file created

PHP: How to get creation date from uploaded file?

Problem: I want to determine the original file creation time from a file uploaded to my server via PHP. My understanding is that the file is copied from the client to a temporary file on my server, which then is referenced in the $_FILES var. The temporary file is of course of no use because it was just created. Is there any way I could get the creation date from the clients original file? Thanks

5 Answers 5

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.

Thanks allot! On a side note: Is it a common practice to put the creation date inside the file content? The file is a .w3g (a replay file associated with Warcraft 3). If there is any chance the file creator put the created date inside the file, I’ll dedicate some time to dissect it further. Just wondering if there is any «common practice» way, to give me a hint of whether or not the date is in there.

No, the stream of data is written to a file in the tmp dir instead of the file being simple ‘copied’ to your webserver, to it’s technically a ‘new’ file.

Читайте также:  php-generated 503

I don’t understand the difference between being streamed and being copied. Aren’t you in both cases simply transferring bytes from one place to another? Or is there a difference in the meta data that is transfered?

Well, it was more to help you ‘see’ the process to make sense. The point is that the created/modified data is saved in the filesystem not the actual file contents. So when the browser sends the file, it just sends the actual file contents and no further information. Flash is really the best option if you need this, as the FileReference class gives you access (client side) to that data that you could manually send with the upload.

In addition to the work-around answers or reliance on embedded information, it is worth noting that the idealised general answer is that if browsers/UAs implement multipart/form-data POST using what’s available in RFC2183.

They can utilise the additional parameters of the Content-Disposition header to add in additional meta data such as creation and modification dates.

I do not know if any browsers do at the moment. But the technical specification is there and as far as I can see it is compatible with RFC2388.

Depending on the type of file, it might be possible: for example, MS Office, Open Office, PDF, and many other types hold a «created date» value within the file properties. although you’d need to open the file and read the relevant information. and it will vary from filetype to filetype

Источник

filemtime

This function returns the time when the data blocks of a file were being written to, that is, the time when the content of the file was changed.

Parameters

Return Values

Returns the time the file was last modified, or false on failure. The time is returned as a Unix timestamp, which is suitable for the date() function.

Errors/Exceptions

Upon failure, an E_WARNING is emitted.

Examples

Example #1 filemtime() example

// outputs e.g. somefile.txt was last modified: December 29 2002 22:16:23.

$filename = ‘somefile.txt’ ;
if ( file_exists ( $filename )) echo » $filename was last modified: » . date ( «F d Y H:i:s.» , filemtime ( $filename ));
>
?>

Notes

Note:

Note that time resolution may differ from one file system to another.

Note: The results of this function are cached. See clearstatcache() for more details.

As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.

See Also

  • filectime() — Gets inode change time of file
  • stat() — Gives information about a file
  • touch() — Sets access and modification time of file
  • getlastmod() — Gets time of last page modification

User Contributed Notes 30 notes

This is a very handy function for dealing with browser caching. For example, say you have a stylesheet and you want to make sure everyone has the most recent version. You could rename it every time you edit it, but that would be a pain in the ass. Instead, you can do this:

By appending a GET value (the UNIX timestamp) to the stylesheet URL, you make the browser think the stylesheet is dynamic, so it’ll reload the stylesheet every time the modification date changes.

To get the last modification time of a directory, you can use this:


$getLastModDir = filemtime("/path/to/directory/.");

Take note on the last dot which is needed to see the directory as a file and to actually get a last modification date of it.

This comes in handy when you want just one ‘last updated’ message on the frontpage of your website and still taking all files of your website into account.

«this is not (necessarily) correct, the modification time of a directory will be the time of the last file *creation* in a directory (and not in it’s sub directories).»

This is not (necessarily) correct either. In *nix the timestamp can be independently set. For example the command «touch directory» updates the timestamp of a directory without file creation.

Also file removal will update the timestamp of a directory.

To get the modification date of some remote file, you can use the fine function by notepad at codewalker dot com (with improvements by dma05 at web dot de and madsen at lillesvin dot net).

But you can achieve the same result more easily now with stream_get_meta_data (PHP>4.3.0).

However a problem may arise if some redirection occurs. In such a case, the server HTTP response contains no Last-Modified header, but there is a Location header indicating where to find the file. The function below takes care of any redirections, even multiple redirections, so that you reach the real file of which you want the last modification date.

// get remote file last modification date (returns unix timestamp)
function GetRemoteLastModified ( $uri )
// default
$unixtime = 0 ;

$fp = fopen ( $uri , «r» );
if( ! $fp )

$MetaData = stream_get_meta_data ( $fp );

foreach( $MetaData [ ‘wrapper_data’ ] as $response )
// case: redirection
if( substr ( strtolower ( $response ), 0 , 10 ) == ‘location: ‘ )
$newUri = substr ( $response , 10 );
fclose ( $fp );
return GetRemoteLastModified ( $newUri );
>
// case: last-modified
elseif( substr ( strtolower ( $response ), 0 , 15 ) == ‘last-modified: ‘ )
$unixtime = strtotime ( substr ( $response , 15 ) );
break;
>
>
fclose ( $fp );
return $unixtime ;
>
?>

There’s a deeply-seated problem with filemtime() under Windows due to the fact that it calls Windows’ stat() function, which implements DST (according to this bug: http://bugs.php.net/bug.php?id=40568). The detection of DST on the time of the file is confused by whether the CURRENT time of the current system is currently under DST.

This is a fix for the mother of all annoying bugs:

function GetCorrectMTime ( $filePath )

$time = filemtime ( $filePath );

$isDST = ( date ( ‘I’ , $time ) == 1 );
$systemDST = ( date ( ‘I’ ) == 1 );

return ( $time + $adjustment );
>
?>

Dustin Oprea

Источник

PHP: how can I get file creation date?

I am reading a folder with lots of files. How can I get the creation date of a file. I don’t see any direct function to get it. There are filemtime and filectime . And if the file hasn’t been modified, what will happen?

4 Answers 4

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.

Returns the time the file was last changed, or FALSE on failure. The time is returned as a Unix timestamp.

@zod If you read a little further than the first lines you may understand more. Go into the comments a little too.

filemtime for Linux is better, more precise, as filectime is changed during owner, permission change asn other operations. You will get more chances to get creation time on Linux using filemtime

Would it be faster to run this php function or to pull a MySQL field for last updated? In my case image path is store in DB and already running a select query

This is the example code taken from the PHP documentation here: https://www.php.net/manual/en/function.filemtime.php

// outputs e.g. somefile.txt was last changed: December 29 2002 22:16:23. $filename = 'somefile.txt'; if (file_exists($filename))

The code specifies the filename, then checks if it exists and then displays the modification time using filemtime() .

filemtime() takes 1 parameter which is the path to the file, this can be relative or absolute.

While this code-only post might answer the question, please add an explanation of why it does so. This will help future readers evaluate the answer for their situation.

The code specifies the filename, then checks if it exists and then displays the modification time using filemtime(). filemtime() takes 1 parameter which is the path to the file, this can be relative or absolute.The example above is copied from the PHP documentation here php.net/manual/en/function.filemtime.php

Unfortunately if you are running on linux you cannot access the information as only the last modified date is stored.

It does slightly depend on your filesystem tho. I know that ext2 and ext3 do not support creation time but I think that ext4 does.

I know this topic is super old, but, in case if someone’s looking for an answer, as me, I’m posting my solution.

This solution works IF you don’t mind having some extra data at the beginning of your file.

Basically, the idea is to, if file is not existing, to create it and append current date at the first line. Next, you can read the first line with fgets(fopen($file, ‘r’)) , turn it into a DateTime object or anything (you can obviously use it raw, unless you saved it in a weird format) and voila — you have your creation date! For example my script to refresh my log file every 30 days looks like this:

if (file_exists($logfile)) < $now = new DateTime(); $date_created = fgets(fopen($logfile, 'r')); if ($date_created == '') < file_put_contents($logfile, date('Y-m-d H:i:s').PHP_EOL, FILE_APPEND | LOCK_EX); >$date_created = new DateTime($date_created); $expiry = $date_created->modify('+ 30 days'); if ($now >= $expiry) < unlink($logfile); >> 

Источник

PHP get create time of directory

Is there anyway to find the created time of a directory in php? I’ve tried filectime but that only works on files.

(sidenote) filectime is change time on Linux. It’s only on Windows that it returns creation time.

9 Answers 9

It should work for directories, this is what I get:

$ php -r "echo filectime(__DIR__);" 1311596297 

Please note Gordon’s comment above: filectime will give you the time of the last change on Unix servers, which might or might not be the same as the creation time.

In unix a folder is also a file. So it should work for that too

$folder = 'includes'; echo date ("F d Y H:i:s.", filemtime($folder)); 

Gathers the statistics of the file named by filename. If filename is a symbolic link, statistics are from the file itself, not the symlink.

It returns the create time as Unix timestamp.

No. Not reliably.

That’s because ctime is not about creation, but change, as pointed out in Gordon’s comment. So while filectime() does work on directories (at least on a Unix machine), chances are it will not give you the date you are looking for. See the notes on the PHP Doc page for filectime() :

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.

In other words, creating a new file in a directory will likely change the ctime of the directory. If you really need to keep track of the creation time, you need to implement the housekeeping yourself.

$filename = 'media/'; echo "$filename was created modified: ".date("F d Y H:i:s.", filectime($filename)); 

if (it dose not work) try clearstatcache(); before your filectime function;

$filename='dirname'; date ("F d Y H:i:s.", filectime($filename)) 

I tested both the function filectime() and filemtime() on a Linux server and they work correctly on directories, too, in the sense that they return a timestamp.

Then, I tried to get the last modified date of a directory, renaming it via FTP and then checking it again, and here’s the strange thing that happened:

  1. the creation date timestamp was updated with the current time
  2. the last modified date was not updated at all

I think this may have occurred because of the FTP file manipulation instead of a direct manipulation through the command line or the system graphical interface.

On Windows, both the creation date and the last modified date do not change when renaming a directory.

For your tests, I also suggest you to have a look at the clearstatcache() function (which I also used in my test) to clear the PHP cache of the filesystem information.

Источник

Оцените статью