Is_writable for processing, remember that PHP may only access files with the username (usually \ ' nobody\ ') running webserver. does not count into the security mode limit.
Example #1 is_writable () example
Copy Code code as follows:
<?php
$filename = ' test.txt ';
if (is_writable ($filename)) {
Echo ' The file is writable ';
} else {
Echo ' The file is not writable ';
}
?>
One problem with the above function is that filename is required. To check the file, must be a file Ah, the directory can not be judged, below we to determine the empty directory.
Instance 1
This feature is very common, especially in some projects that need to generate static files, whether a directory can be related to the directory has to create files to delete the file permissions
Copy Code code as follows:
/*
Problem arises: How to check whether a directory can be written, how to directory and files, then check
Ideas:
(1) First write the algorithm to check whether the empty directory is writable:
Generates a file in the directory, if it cannot be generated, indicating that the directory does not have write permissions
(2) The use of recursive methods to check
Code implementation:
*/
Set_time_limit (1000);
function check_dir_iswritable ($dir _path) {
$dir _path=str_replace (' \ ', '/', $dir _path);
$is _writale=1;
if (!is_dir ($dir _path)) {
$is _writale=0;
return $is _writale;
}else{
$file _hd= @fopen ($dir _path. /test.txt ', ' W ');
if (! $file _HD) {
@fclose ($file _HD);
@unlink ($dir _path. /test.txt ');
$is _writale=0;
return $is _writale;
}
$dir _hd=opendir ($dir _path);
while (false!== ($file =readdir ($dir _hd))) {
if ($file!= "." && $file!= "...") {
if (Is_file ($dir _path. ') /'. $file)) {
File not writable, return directly
if (!is_writable ($dir _path. ') /'. $file)) {
return 0;
}
}else{
$file _hd2= @fopen ($dir _path. /'. $file. ' /test.txt ', ' W ');
if (! $file _hd2) {
@fclose ($file _hd2);
@unlink ($dir _path. /'. $file. ' /test.txt ');
$is _writale=0;
return $is _writale;
}
Recursion
$is _writale=check_dir_iswritable ($dir _path. /'. $file);
}
}
}
}
return $is _writale;
}
The above example is mainly fopen to create files in the directory or write content in the file, so that you can determine the directory read and write permissions.