Home >Backend Development >PHP Problem >How to quickly know the size, type and permissions of the file in php file operations
In the previous article "php file operation: how to obtain the time attribute (information) of the file", we learned that we can use the built-in functions filectime(), filemtime, fileatime() to obtain separate files Three time attributes: creation time, modification time and last access time. Interested friends can learn about it~
The main content of this article is to understand the file size, file type and file/directory permissions method of obtaining. Let’s give you a detailed introduction to how to obtain it.
1. Get the size of the file
You can use the filesize($filename)
function to return the specified file$filename
the size of. Returns the file size in bytes on success or FALSE on failure.
<?php header("content-type:text/html;charset=utf-8"); $filename = "test.txt"; echo "{$filename} 文件的大小为:".filesize($filename); ?>
The output result is:
2. Get the file type
You can use filetype ($filename)
The function returns the type of the specified file or directory $filename
. If successful, one of the following 7 possible values will be returned:
fifo
<?php header("content-type:text/html;charset=utf-8"); $filename = "D:/demo/text.txt"; echo "{$filename} 文件的类型为:".filetype($filename); $filename = "D:/demo/1.jpg"; echo "<br>{$filename} 文件的类型为:".filetype($filename); $filename = "D:/demo/img"; echo "<br>{$filename} 文件的类型为:".filetype($filename); ?>
The output result is:
3. Obtain file permissionsYou can use
fileperms ($filename) The function returns the permissions of the file or directory $filename
. If successful, the permissions are returned as a number. On failure, returns FALSE. <pre class="brush:php;toolbar:false"><?php
header("content-type:text/html;charset=utf-8");
$filename = "test.txt";
echo "{$filename} 文件的权限为:".fileperms($filename);
?></pre>
The output result is:
# Description: The number representing permissions has three possible values. To set multiple permissions, you can use the following numbers. Total:
The above is the detailed content of How to quickly know the size, type and permissions of the file in php file operations. For more information, please follow other related articles on the PHP Chinese website!