How toTech

How to delete a file in PHP?

Delete a File in PHP: There are some set of functions linked with each task on any programming language. Similarly, the PHP language too has some set of functions associated with each task linked. Thereby, to delete a file in PHP you need to apply a function and run it. In this article, we will get to know how to delete a file in PHP.

As we all know, PHP is a cross-platform supported programming language it can run on various operating systems. PHP is widely used because of its easily accessible nature. Because of the similar syntax as that of C, PHP is pretty simple to use even by a beginner. Since speed is the main key factor in any coding platform, PHP proves to be the fastest programming language.

Delete a File in PHP

When you view the contents of a directory you can see all the files that exist in that directory because the operating system or application that you are using displays a list of filenames. You can think of these filenames as links that join the files to the directory you are currently viewing. If you unlink a file, you are effectively causing the system to forget about it or delete it! Before you can delete (unlink) a file, you must first be sure that it is not open in your program. Use the f close function to close down an open file.

To delete a file in PHP is very uncommon but in case if you want to do so, you can use the unlink() function in PHP. Once you delete a file you cannot undo it, so make sure that you delete the right file and also think twice before doing so. This unlink() function in PHP is a built-in which allows you to delete a file. The filename of the file which you want to delete is sent as a parameter and the function returns True on success and False on failure.

Also, you can learn how to prevent SQL injection in PHP. To learn more in regard to this topic explore the link highlighted below.
How to prevent SQL injections in PHP

Syntax

The syntax of the unlink() function is as follows,

unlink(filename, context) 

Parameters

Here are a few parameters of the unlink() function. The unlink() function uses two parameters.
1) Filename: This is a mandatory parameter which specifies the file name of which has to be deleted.
2) Context: This is an optional parameter which specifies the context of the file handle which can be used to modify the nature of the stream.

Return value

This function returns true on success and false on failure.

Errors and Exceptions

Here are some errors and exceptions in the unlink() function in PHP.

  • The unkink() function generates an E_WARNING level error on failure.
  • The web server user must have write permissions to the directory for using the unlink() function.
  • The unlink() function returns Boolean False but many times it happens that it returns a non-Boolean value which evaluates to False.

Below are a few programs which illustrate the unlink() function.

You can also learn how you can link or compare arrays in PHP. To know more in regard to this topic you can tap on the highlighted link below.
How to compare two arrays in PHP

Program 1: PHP program to delete a file named gfg.txt using unlink() function.

<?php 
// PHP program to delete a file named gfg.txt 
// using unlike() function

$file_pointer= "gfg.txt";
// using unlink() function to delete a file
if(!unlink($file_pointer))
{
echo("$file_pointer cannot be deleted due to an error");
}
else
{
echo("$file_pointer has been deleted");
}
?>

Output:The output of the above illustrated example is

1

Program 2: Deleting a file named gfg.txt using unlike() function

<?php 
// PHP program to delete a file named gfg.txt 
// using unlike() function

$file_pointer= fopen('gfg.txt');

// writing on a file named gfg.txt
fwrite($file_pointer, 'A computer science portal for geeks!'); fclose($file_pointer);
// using unlink() function to delete a file
unlink('gfg.txt');

?>

Output: The output of the above illustrated example is as follows,

1

FAQ’s

Here are some top 3 FAQ’s which can clear up your mind on any doubts in regard to this topic. Scroll down below to know more,

What is the difference between unset and unlink in PHP?

In PHP unlink() is a function for file system handling, unlink() is used to delete files. Suppose you have uploaded a file and wants to delete this file through the coding then unlink() function is used to delete the file. unset() is a function for variable management or Unset () is used to destroy a variable in PHP.

What is isset in PHP?

The isset () function is used to check whether a variable is set or not. If a variable is already unset with unset() function, it will no longer be set. The isset() function return false if testing variable contains a NULL value

Why do we use Get and Post method in PHP?

GET can’t be used to send binary data, like images or word documents, to the server. The data sent by GET method can be accessed using the QUERY_STRING environment variable. The PHP provides a $_GET associative array to access all the sent information using GET method. The POST method transfers information via HTTP headers.

To conclude: Hence, through this article we have learnt how you can delete a file in PHP. If you have any queries in regard to this content comment them below. Also, comment your views below in the comment section and do share the content if you like it. For some more interesting tech articles and news visit Morphigo.com and delve into some amazing stuff on the web.

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button
Close