Skip to main content

PHP File Handling: Examining File Details


In this tutorial, 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:

  1. <?php
  2.  
  3. $testfile = 'hello.txt';
  4. echo filesize($testfile);// this will echo in byte
  5.  
  6. //this next line are the mdifcation time, and its more than just number
  7. //filemtime: last modified (changed content). and its the file modifiaction time
  8. echo strftime('%m%d%Y %H:%M', filemtime($testfile)).'<br/>';
  9. //filectime: last changed (changed content or metadata)or the last change time of the content of metadata
  10. echo strftime('%m%d%Y %H:%M', filectime($testfile)).'<br/>';
  11. //fileatime: last accessed (any read/change). it keeps track when a file is last accessed
  12. echo strftime('%m%d%Y %H:%M', fileatime($testfile)).'<br/>';
  13. ?>

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.

  1. <?php
  2.  
  3. $testfile = 'hello.txt';
  4. echo filesize($testfile).'<br/>';// this will echo in byte
  5.  
  6.  
  7. //this next line are the mdifcation time, and its more than just number
  8. //filemtime: last modified (changed content). and its the file modifiaction time
  9. echo strftime('%m/%d/%Y %H:%M', filemtime($testfile)).'<br/>';
  10. //filectime: last changed (changed content or metadata)or the last change time of the content of metadata
  11. echo strftime('%m/%d/%Y %H:%M', filectime($testfile)).'<br/>';
  12. //fileatime: last accessed (any read/change). it keeps track when a file is last accessed
  13. echo strftime('%m/%d/%Y %H:%M', fileatime($testfile)).'<br/>';
  14.  
  15. touch($testfile);
  16. //this next line are the mdifcation time, and its more than just number
  17. //filemtime: last modified (changed content). and its the file modifiaction time
  18. echo strftime('%m/%d/%Y %H:%M', filemtime($testfile)).'<br/>';
  19. //filectime: last changed (changed content or metadata)or the last change time of the content of metadata
  20. echo strftime('%m/%d/%Y %H:%M', filectime($testfile)).'<br/>';
  21. //fileatime: last accessed (any read/change). it keeps track when a file is last accessed
  22. echo strftime('%m/%d/%Y %H:%M', fileatime($testfile)).'<br/>';
  23.  
  24.  
  25. //in this line of code we're going to get back the file info using pathinfo
  26. $pathinfo = pathinfo(__FILE__);
  27. echo $pathinfo['dirname'].'<br/>';// returns the directory name
  28. echo $pathinfo['basename'].'<br/>';// returns file details
  29. echo $pathinfo['filename'].'<br/>';// returns file_details.php
  30. echo $pathinfo['extension'].'<br/>';// returns php
  31. ?>

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

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.