PHP to determine whether the file empty directory has read and write access to the function code _php tips

Source: Internet
Author: User
Tags file permissions
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.