Home  >  Article  >  Backend Development  >  How to determine whether file deletion is successful in php

How to determine whether file deletion is successful in php

青灯夜游
青灯夜游Original
2021-11-01 18:35:372776browse

In PHP, you can use the if statement and the unlink() function to determine whether the file is successfully deleted. The syntax is "if(unlink($file)){echo 'File deleted successfully!';}else{echo ' File deletion failed!';}".

How to determine whether file deletion is successful in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In php, if you want to delete files, you need Use the unlink() function.

The unlink() function can delete the specified file. It returns TRUE when the function is executed successfully (the file is deleted successfully), and FALSE when it fails.

If you want to determine whether the file is deleted successfully, you can use the if statement. If the unlink() function returns TRUE, it will output a prompt that the file was successfully deleted; if it returns FALSE, it will output a prompt that the file failed to be deleted.

Implementation code:

<?php
header("Content-type:text/html;charset=utf-8");
$file = &#39;newtest.txt&#39;;
if (file_exists($file)) {
	if (unlink($file)) {
		echo &#39;文件删除成功!&#39;;
	} else {
		echo &#39; 文件删除失败!&#39;;
	}
} else {
	echo $file . &#39; 不存在!&#39;;
}
?>

Output result:

How to determine whether file deletion is successful in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to determine whether file deletion is successful 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