How to delete files in php

Guanhui
Release: 2023-04-08 16:14:02
Original
5436 people have browsed it

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 {
  //文件不存在
}
Copy after login

Then check whether you have permission to delete, if not, use the function "chmod" to modify the permission;

//修改权限
chmod($filename,0777);
Copy after login

Finally, use the function "unlink" to delete the file.

//删除文件
if (unlink('./filename.txt')) {
  //删除成功
} else {
  //删除失败
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template