How To Delete Image From Folder In PHP
Last Updated : Mar 11, 2024
IN - PHP | Written & Updated By - Anjali
In this article we will show you the solution of how to delete image from folder in PHP, we will see two examples to delete an image from a folder using the php unlink() function.
Let us know about unlink() first. Unlink() is the function in php it is used to delete files from a folder.
Syntax,
Unlink(filename, context)
Step By Step Guide On How To Delete Image From Folder In PHP :-
Example 1
In this example, we delete the image from a root folder using unlink() function.
<!DOCTYPE html> <html lang = " en " > <head> <meta charset = " UTF - 8" > <meta http-equiv = " X-UA-Compatible " content = " IE=edge " > <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " > <title> how to delete image from folder in php </title> </head> <body> <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1> <h2> how to delete image from folder in php </h2> <form action="deletefile.php" method="post"> <Button type="submit" name="submit"> DELETE FILE </Button> </form> </body> </html> <?php $path = "uploads/image.jpg" ; if (!unlink ($path)) { echo "you have an error ! " ; } else { header ("location: index.php?deletesuccess") ; } ?>
- First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here and also adding the inline CSS here.
- Create a <form> with the action of the php file with the method post.
- Creating an <button> of type submit and name submit.
- Now we close the HTML file with </html> tag.
- We write <?php tag to write PHP within it.
- Creating a variable $path to add the path of the image
- Using an if statement for if an unlink the image faces an error, echo to display the error message.
- Now use an else statement to delete the success message to header().
- ?> to close the php code.
Example 2
In the example below, we used unlink() to delete file from any folder.
<!DOCTYPE html> <html lang = " en " > <head> <meta charset = " UTF - 8" > <meta http-equiv = " X-UA-Compatible " content = " IE=edge " > <meta name = " viewport " content = " width = device-width , initial-scale = 1.0 " > <title> how to delete image from folder in php </title> </head> <body> <h1 style=" color : rgb(113, 221, 113) ;"> TALKERSCODE </h1> <h2> how to delete image from folder in php </h2> <form action="deletefile.php" method="post"> File Path: <input type="text" name="fileToRemove"><br> <input type="submit" name="remove_file" value="Delete Image"> </form> </body> </html> <?php if(isset($_POST['remove_file'])) { $file_path = $_POST['fileToRemove'] ; if(file_exists($file_path)) { unlink($file_path) ; echo 'file deleted' ; } else { echo 'file not exists' ; } } ?>
- First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
- Secondly, the <html> tag is used to indicate the beginning of an HTML document.
- As mentioned above, the <head> tag contains information about the web page. In this tag, a <title> tag is used which helps us to specify a webpage title.
- Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
- Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
- <h1> tag used to add heading here and also adding the inline CSS here.
- Create a <form> with the action of the php file with the method post.
- <input> tag used to enter a file path.
- Another <input> with the type ‘submit’.
- Now we close the HTML file with </html> tag.
- We write <?php tag to write PHP within it.
- Using an if statement with isset() with $_POST with the name of the submit button
- $file_path to add the file path with $_POST method.
- Another if else statement for if the file exists then using unlink() to delete the file, using echo to see the success message or else display the error message.
- ?> to close the php code.
Conclusion :-
At last, here in conclusion, here we can say that with this article’s help, we know how to delete image from folder in php.
I hope this article on how to delete image from folder in PHP helps you and the steps and method mentioned above are easy to follow and implement.