PHP file operation: check whether the file can be read/written/executed

青灯夜游
Release: 2023-04-10 13:46:01
Original
4580 people have browsed it

In the previous article "php file operation: reading files line by line", we introduced two methods of reading files line by line in PHP (fgets() and fgetss() functions) . The following article will introduce to you how to use PHP to check whether a file is readable, writable, and executable.

When performing file reading and writing operations, you need to first check whether the file can be read, written, or executed, that is, whether the file has readable, writable, and executable permissions. So how to check? Today we will introduce to you the inspection method.

We have a text file named "test.txt", use the chmod() function to set permissions:

<?php
chmod("test.txt", 0400);
?>
Copy after login

The above code can set the test.txt file permissions to: owner Readable, no one else has permission.

Let’s take a look at how to check whether a file is readable, writable, and executable.

1. Use the is_readable() function to check whether the file is readable

The is_readable() function can check whether the specified file is readable; this function only accepts one parameter$filename, used to specify the file to be checked.

Let’s take a look at the code example:

 <?php
 header("Content-type:text/html;charset=utf-8");
 $file = "test.txt";
 if(is_readable($file))
 {
 echo ("$file 可读");
 }
 else
 {
 echo ("$file 不可读");
 }
 ?>
Copy after login

If the $filename file exists and the data can be read, TRUE is returned, otherwise FALSE is returned. Therefore, the output result is:

test.txt 可读
Copy after login

2. Use the is_writable() function to check whether the file is writable

The is_writable() function can check whether the specified file is writable.

This function also accepts a parameter $filename, which is used to specify the file to be checked; if the $filename file exists and the data can be read, TRUE is returned. Otherwise return FALSE.

Let’s take a look at the code example below:

 <?php
 header("Content-type:text/html;charset=utf-8");
 $file = "test.txt";
 if(is_writable($file))
 {
 echo ("$file 可写");
 }
 else
 {
 echo ("$file 不可写");
 }
 ?>
Copy after login

Output result:

test.txt 不可读
Copy after login

3. Use the is_executable() function to check whether the file is executed

The is_executable() function can check whether the specified file is executable; this function also accepts a parameter $filename.

 <?php
 header("Content-type:text/html;charset=utf-8");
 $file = "test.txt";
 if(is_executable($file))
 {
 echo ("$file 可执行");
 }
 else
 {
 echo ("$file 不可执行");
 }
 ?>
Copy after login

Returns TRUE if $filename file exists and data can be read, otherwise returns FALSE. Therefore, the output result is:

test.txt 不可执行
Copy after login

Explanation:

The results of the is_readable(), is_writable() and is_executable() functions will be cached and clearstatcache needs to be used () function to clear the cache.

Add the following code at the end of the program to clear the cache:

clearstatcache();
Copy after login

That’s all. If you want to know anything else, you can click this. → →Basic operation of PHP files

## Recommended: PHP interview questions summary (collection)》《php video tutorial

The above is the detailed content of PHP file operation: check whether the file can be read/written/executed. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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