Home  >  Article  >  Backend Development  >  PHP file programming (1)-two ways to obtain file information

PHP file programming (1)-two ways to obtain file information

WBOY
WBOYOriginal
2016-07-25 08:59:28938browse
  1. //Open the file

  2. $file_path="text.txt";
  3. if($fp=fopen($file_path,"r")){
  4. //Take out File information
  5. $file_info=fstat($fp);
  6. echo "
    ";</li>
    <li> print_r($file_info);</li>
    <li> echo "
    ";

  7. / /Get out individually

  8. $file_size=$file_info['size'];
  9. //The file size is calculated in bytes
  10. echo "The size of the file is:".$file_size;
  11. echo "
    File The time of last access: ".date("Y-m-d H:i:s",$file_info['atime']); //atime means [the time when the file was last accessed]
  12. echo "
    echo "< br/>The time when the file was last changed:".date("Y-m-d H:i:s",$file_info['ctime']); //ctime means [the file was last modified by the file owner/file group Time】

  13. }else{

  14. echo "Failed to open file";
  15. }

  16. //Close the file, this is very important

  17. fclose($fp);
  18. ?>

Copy the code

Method 2 to get file information

  1. //The second way to get file information by bbs.it-home.org
  2. $file_path="text.txt";
  3. if(!file_exists($file_path )){
  4. echo "File does not exist";
  5. exit();
  6. }

  7. echo "
    ".date("Y-m-d H:i:s",fileatime($ file_path));

  8. echo "
    ".date("Y-m-d H:i:s",filemtime($file_path));
  9. echo "
    ".date("Y-m-d H:i:s" ,filectime($file_path));

  10. //echo "
    ".filemtime($file_path);

  11. //echo "
    ".filectime($file_path) ;
  12. ?>

Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn