Home > Backend Development > PHP Problem > How to quickly know the size, type and permissions of the file in php file operations

How to quickly know the size, type and permissions of the file in php file operations

青灯夜游
Release: 2023-03-12 07:38:02
Original
2803 people have browsed it

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);
?>
Copy after login

The output result is:

How to quickly know the size, type and permissions of the file in php file operations

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

  • ##char

  • ##dir
  • block
  • link
  • file
  • unknown
  • If failed, return FALSE.
<?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);

?>
Copy after login

The output result is:

How to quickly know the size, type and permissions of the file in php file operations

3. Obtain file permissions

You 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.

<?php
header("content-type:text/html;charset=utf-8");
$filename = "test.txt";
echo "{$filename} 文件的权限为:".fileperms($filename);
?>
Copy after login
The output result is:

How to quickly know the size, type and permissions of the file in php file operations# Description: The number representing permissions has three possible values. To set multiple permissions, you can use the following numbers. Total:

    1 = Execute permission
  • #2 = Write permission
  • 4 = Read permission
  • PHP Chinese website platform has a lot of video teaching resources. Welcome everyone to learn "
PHP Video Tutorial

"!

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!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template