Home  >  Article  >  Backend Development  >  How to delete files in php

How to delete files in php

Guanhui
GuanhuiOriginal
2020-05-06 17:29:565430browse

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!

Statement:
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