How to use the unlink function to determine permissions for deleting files in php _ PHP Tutorial

Source: Internet
Author: User
Delete files in php using the unlink function. The unlink function is used to delete files in php, but we can determine whether the file has the permission to be deleted. next I will give you a brief introduction to the unlink function usage and permission judgment. The unl unlink function deletes a file in php. However, we can determine whether the file has the permission to be deleted. Next, I will give you a brief introduction to the unlink function usage and permission judgment.

Unlink ($ file)

Example 1

The code is as follows:

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

Sometimes there may be permission issues, and the file has the write permission, but cannot be deleted.

The reason is simple. deleting a file is not a modification to the file, but a modification to the directory. you must have the write permission to the directory where the file is located.

The is_readable () function is used to determine whether the file has the read permission. the is_writable () function is used to determine whether the file has the write permission. The is_readable () function is used to determine whether the specified file is readable. The syntax is as follows:
Bool is_readable (string filename)
Returns TRUE if the object exists and is readable.
Is_writable () function to determine whether a specified file can be written. The syntax is as follows:
Bool is_writable (string filename)
Returns TRUE if the file exists and is writable. The filename parameter can be a directory name that allows write check.
Note: PHP may only use the user name (usually 'nobody') for running webserver to access the file. This parameter is not included in the security mode. The judgment of the file permission is the premise for operations on the file, especially when reading, writing, renaming, and other operations on the file, if the file does not have the backup read and write permissions, these operations are meaningless.
Design process
(1) create the index. php file.
(2) add a form, set the text box, submit the specified file, set the file domain, submit the data written to the file, set the submit button, and submit the data to this page using the POST method.
(3) use the $ _ POST [] method to obtain the file path and content submitted by the form, and use the iconv () function to encode and convert the obtained data. First, determine whether the specified file exists. Then, determine whether the specified file has the write permission. If yes, write the file content submitted in the form to the file. Finally, close the opened file and give a prompt. The key code of index. php is as follows:

The code is as follows:
If ($ _ POST ['File _ name']! = "" & Is_file (iconv ("UTF-8", "gb2312", $ _ POST ['File _ name']) = true) {// Determine whether the file exists
$ File_name = iconv ("UTF-8", "gb2312", $ _ POST ['File _ name']); // Encode and convert
If (file_exists ($ file_name )){
If (is_writable ($ file_name) {// checks whether the file has the write permission
$ Fp = fopen ($ file_name, "w +"); // open the specified file
If (fwrite ($ fp, $ file_content) {// write operation
Echo "script" alert ('file written successfully! '); Script ";
} Else {
Echo "script" alert ('file writing failed! '); Script ";
}
Fclose ($ fp); // close the file
} Else if (is_readable ($ file_name) {// checks whether the object has the read permission
Echo "script" alert ('file only has read permission! '); Script ";
} Else {
Echo "script" alert ('The file does not have read and write permissions! '); Script ";
}
} Else {
Echo "script" alert ('The file does not exist! '); Script ";
}
} Else {
Echo "script" alert ('Enter the correct file path! '); Script ";
}
?>

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