Home  >  Article  >  Backend Development  >  How to get file attributes in PHP? Introduction to the method of getting file attributes in PHP (with code)

How to get file attributes in PHP? Introduction to the method of getting file attributes in PHP (with code)

不言
不言Original
2018-07-23 15:20:504530browse

PHP can use a variety of functions to obtain file attributes. Each function allows us to obtain various information about the file. Next, let's take a look at the different methods of obtaining file attributes in PHP.

PHP gets file attributes to get the latest modification time:

< ?php  
$file = &#39;test.txt&#39;;  
echo date(&#39;r&#39;, filemtime($file));  
?>

The returned timestamp is the unix timestamp, which is commonly used in caching technology.

Related PHP gets file attributes There is the time fileatime() and filectime() are used to obtain the last accessed time. When the file's permissions, owner, all groups or other metadata in the inode are updated, the fileowner() function returns the file owner

$owner = posix_getpwuid(fileowner($file));

(Non-window system), ileperms() gets the permissions of the file,

< ?php  
$file = &#39;dirlist.php&#39;;  
$perms = substr(sprintf(&#39;%o&#39;, fileperms($file)), -4);  
echo $perms;  
?>

filesize() returns the number of bytes of the file size:

< ?php  
// 输出类似:somefile.txt: 1024 bytes  
$filename = &#39;somefile.txt&#39;;  
echo $filename . &#39;: &#39; . filesize($filename) . &#39; bytes&#39;;  
?>

PHP gets all the information about the file attributes and returns Array function stat() function:

< ?php  $file = &#39;dirlist.php&#39;;  $perms = stat($file);  var_dump($perms);  ?>

Related recommendations:

PHP How to get the protected attribute

The above is the detailed content of How to get file attributes in PHP? Introduction to the method of getting file attributes in PHP (with code). For more information, please follow other related articles on the PHP Chinese website!

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