Home > Article > Backend Development > Deleting files in PHP uses the unlink function to determine permissions_PHP tutorial
The unlink function in PHP deletes files, but we can determine whether the file has deletion permission. Let me briefly introduce the usage of the unlink function and permission judgment.
unlink($file)
Example 1
The code is as follows | Copy code | ||||
or die("Cannot delete file.") ?> |
Sometimes there is a permission problem. You have write permission to the file, but you cannot delete it.
The reason is very simple. Deleting a file is not a modification of the file, but a modification of the directory. You need to have write rights to the directory where the file is located
The is_readable() function is used to determine whether the file has read permission, and the is_writable() function is used to determine whether the file has write permission. The is_readable() function determines whether the specified file is readable. The syntax is as follows:
bool is_readable(string filename)
Returns TRUE if the file exists and is readable.
The is_writable() function determines whether the specified file is writable. The syntax is as follows:
bool is_writable(string filename)
Returns TRUE if the file exists and is writable. The parameter filename can be a directory name that allows writable checking.
Note: PHP may only be able to access files under the username the webserver is running under (usually 'nobody'). Does not count towards Safe Mode limits. Judging file permissions is the prerequisite for operating files, especially when performing file reading, writing, renaming and other operations. If the file does not have read and write permissions, then these operations are meaningless. of.
Design Process
(1) Create index.php file.
(2) Add a form, set the text box, submit the specified file, set the file domain, submit the data written in the file, set the submit button, and use the POST method to submit the data to this page.
(3) Obtain the file path and file content submitted by the form through the $_POST[] method, and encode the obtained data through the iconv() function. First, determine whether the specified file exists. Then, determine whether the specified file has write permission, and if so, write the content of the file submitted by the form into the file. Finally, close the open file and give a prompt message. The key code of index.php is as follows:
代码如下 | 复制代码 |
if($_POST['file_name']!=""&& is_file(iconv("utf-8","gb2312",$_POST['file_name']))==true){ //判断文件是否存在 $file_name=iconv("utf-8","gb2312",$_POST['file_name']); //编码转换 if(file_exists($file_name)){ if(is_writable($file_name)){ //判断文件是否具备写的权限 $fp=fopen($file_name,"w+"); //打开指定的文件 if(fwrite($fp,$file_content)){ //执行写入的操作 echo"<script>alert('文件写入成功!');</script>"; }else{ echo"<script>alert('文件写入失败!');</script>"; } fclose($fp); //关闭文件 }else if(is_readable($file_name)){ //判断文件是否具备读的权限 echo"<script>alert('文件只具备读权限!');</script>"; }else{ echo"<script>alert('文件不具备读、写权限!');</script>"; } }else{ echo"<script>alert('文件不存在!');</script>"; } }else{ echo"<script>alert('请输入正确的文件路径!');</script>"; } ?> |