Unlink ($ file)
Example 1
The code is as follows: |
Copy code |
<? Php 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: |
Copy code |
<? Php 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> "; } ?>
|