PHP Delete files with unlink function permissions to judge _php Tutorial

Source: Internet
Author: User
unlink function in PHP is to delete the file, but we can determine whether the file has delete permissions, let me give you a brief introduction to the Unlink function usage and permission to judge.

Unlink ($file)

Example 1

The code is as follows Copy Code

Unlink ($somefile)
Or Die ("Cannot delete file.")
?>

Sometimes permissions problems occur, write permissions to files, but cannot be deleted.

The reason is simple, delete the file is not to modify the file, but to modify the directory, you need to write to the directory where the file

The is_readable () function is applied to determine whether a file has Read permissions, and the Is_writable () function is used to determine whether the file has write permissions. The is_readable () function, which determines whether the specified file is readable, has the following syntax:
bool Is_readable (string filename)
Returns True if the file exists and is readable. The
Is_writable () function, which determines whether the specified file is writable, has the following syntax:
bool Is_writable (string filename)
Returns True if the file exists and is writable. The parameter filename can be a directory name that allows for a writable check.
Note: PHP may only be able to access the file by running the webserver user name (typically ' nobody '). Restrictions that do not count toward Safe mode. The judgment of the file permissions is the premise of the operation of the file, especially in the execution of the file read, write, rename, and so on, if the file does not have the permission to read, write, then these operations are meaningless. The
Design process
(1) creates a index.php file.
(2) Add the form, set the text box, submit the specified file, set the file field, submit the data written to the file, set the Submit button, and use the Post method to submit the data to this page. The
(3) obtains the file path and file contents of the form submission through the $_post[] method, and encodes the obtained data through the Iconv () function. First, determine whether the specified file exists. It then determines whether the specified file has write permissions, and if so, writes the contents of the file submitted by the form to the file. Finally, close the open file and give a hint. The key code for index.php is as follows:

The code is as follows Copy Code
if ($_post[' file_name ']!= "" && Is_file (Iconv ("Utf-8", "gb2312", $_post[' file_name '))) {//Determine if the file exists
$file _name=iconv ("Utf-8", "gb2312", $_post[' file_name ']); Encoding Conversion
if (file_exists ($file _name)) {
if (is_writable ($file _name)) {//Determine if the file has Write permission
$FP =fopen ($file _name, "w+"); Opens the specified file
if (fwrite ($fp, $file _content)) {//Perform write operation
echo "";
}else{
echo "";
}
Fclose ($FP); Close File
}else if (is_readable ($file _name)) {//Determine if the file has Read permissions
echo "";
}else{
echo "";
}
}else{
echo "";
}
}else{
echo "";
}
?>

http://www.bkjia.com/PHPjc/631499.html www.bkjia.com true http://www.bkjia.com/PHPjc/631499.html techarticle unlink function in PHP is to delete the file, but we can determine whether the file has delete permissions, let me give you a brief introduction to the Unlink function usage and permission to judge. UNL ...

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