Home > Article > Backend Development > How to delete files in php
php method to delete files
First of all, generally use the "file_exists" function before deleting a file to determine whether the file exists. If it exists, then Delete;
$filename = '/test.jpg'; if (file_exists($filename)) { //文件存在 } else { //文件不存在 }
Then check whether you have permission to delete, if not, use the function "chmod" to modify the permission;
//修改权限 chmod($filename,0777);
Finally, use the function "unlink" to delete the file.
//删除文件 if (unlink('./filename.txt')) { //删除成功 } else { //删除失败 }
The above is the detailed content of How to delete files in php. For more information, please follow other related articles on the PHP Chinese website!