10 – PHP File Handling: Examining File Details

0
639

In this tutrial, I’m going to show how to get information about the file details. To start in this lesson lets create a new PHP file called “file_detail.php”. And add the following code:
[php] ‘;

//filectime: last changed (changed content or metadata)or the last change time of the content of metadata
echo strftime(‘%m%d%Y %H:%M’, filectime($testfile)).’
‘;

//fileatime: last accessed (any read/change). it keeps track when a file is last accessed
echo strftime(‘%m%d%Y %H:%M’, fileatime($testfile)).’
‘; ?>
[/php] When executing the code above, it will return the size of a file in bytes and all the modification time are all the same. And it looks like as shown below.

 Output:

 13
01/09/2014 14:37
01/03/2014 02:53
01/09/2014 14:37

Next, on that we can modify those time is using the touch command, so that we can update those into the current time and we can also pass in  some additional arguments. But this were going to set it to the current system time and flip all of these time to the current time. and here’s the following code.
[php] ‘;
// this will echo in byte
//this next line are the mdifcation time, and its more than just number
//filemtime: last modified (changed content). and its the file modifiaction time

echo strftime(‘%m/%d/%Y %H:%M’, filemtime($testfile)).’
‘;

//filectime: last changed (changed content or metadata)or the last change time of the content of metadata

echo strftime(‘%m/%d/%Y %H:%M’, filectime($testfile)).’
‘;

//fileatime: last accessed (any read/change). it keeps track when a file is last accessed

echo strftime(‘%m/%d/%Y %H:%M’, fileatime($testfile)).’
‘;
touch($testfile);

//this next line are the mdifcation time, and its more than just number
//filemtime: last modified (changed content). and its the file modifiaction time

echo strftime(‘%m/%d/%Y %H:%M’, filemtime($testfile)).’
‘;

//filectime: last changed (changed content or metadata)or the last change time of the content of metadata

echo strftime(‘%m/%d/%Y %H:%M’, filectime($testfile)).’
‘;

//fileatime: last accessed (any read/change). it keeps track when a file is last accessed

echo strftime(‘%m/%d/%Y %H:%M’, fileatime($testfile)).’
‘;

//in this line of code we’re going to get back the file info using pathinfo

$pathinfo = pathinfo(__FILE__); echo $pathinfo[‘dirname’].’
‘;
// returns the directory name echo $pathinfo[‘basename’].’
‘;

// returns file details echo $pathinfo[‘filename’].’
‘;
// returns file_details.php echo $pathinfo[‘extension’].’
‘;
// returns php
?>
[/php]

Output:

 16
01/09/2014 14:37
01/03/2014 02:53
01/09/2014 14:37
01/09/2014 14:37
01/03/2014 02:53
01/09/2014 14:37
C:\xampp\htdocs\file
file_detail.php
file_detail
php
Facebook Comments

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.