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); ?>
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 不可读"); } ?>
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 可读
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 不可写"); } ?>
Output result:
test.txt 不可读
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 不可执行"); } ?>
Returns TRUE if $filename file exists and data can be read, otherwise returns FALSE. Therefore, the output result is:
test.txt 不可执行
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();
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!