ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

10 – PHP File Handling: Examining File Details

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:

  1. <?php $testfile = 'hello.txt';
  2. echo filesize($testfile);
  3. // this will echo in byte
  4. //this next line are the mdifcation time, and its more than just number
  5. //filemtime: last modified (changed content). and its the file modifiaction time
  6.  
  7. echo strftime('%m%d%Y %H:%M', filemtime($testfile)).'<br/>';
  8.  
  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.  
  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/>'; ?>

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

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

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!