codescracker


php

PHP Delete File



« Previous Tutorial Next Tutorial »


To delete any file in PHP, use unlink() function.

PHP Delete File Syntax

Here is the general form of deleting a file in PHP.

unlink($file_name);

PHP Delete File Example

Before delete any file in PHP, let's first see the screenshot of C:\xampp\htdocs\ directory folder. Below is the screenshot of this directory.

php delete file

As you can see, there is a file named codescracker is present inside the current directory which is a textual file.

Now, here is an example of deleting a file in PHP

<html>
<head>
	<title>Deleting a file in PHP</title>
</head>
<body>
<?php
	$file_name = "codescracker.txt";
	echo "<p>Deleting the file <b>".$file_name."</b>.....</p>";
	unlink($file_name);
	echo "<p>File deleted successfully.</p>";
?>
</body>
</html>

Here is the sample output produced by the above deleting file example code in PHP.

deleting a file php

And here is the screenshot of the current directory folder.

php delete file example

As you can see, the file codescracker.txt is deleted from the current directory.


« Previous Tutorial Next Tutorial »








Tools
Calculator

Quick Links
Signup - Login - Give Online Test