All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Delete Image From Folder In PHP

Last Updated : Jul 1, 2023

How To Delete Image From Folder In PHP

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") ;
}
?>
  1. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. 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.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here and also adding the inline CSS here.
  7. Create a <form> with the action of the php file with the method post.
  8. Creating an <button> of type submit and name submit.
  9. Now we close the HTML file with </html> tag.
  10. We write <?php tag to write PHP within it.
  11. Creating a variable $path to add the path of the image
  12. Using an if statement for if an unlink the image faces an error, echo to display the error message.
  13. Now use an else statement to delete the success message to header().
  14. ?> 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' ;
    }
}
?>
  1. First, we write <! DOCTYPE html> which we used as the instruction to the web browser about what version of HTML file is written in.
  2. Secondly, the <html> tag is used to indicate the beginning of an HTML document.
  3. 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.
  4. Both <head> and <title> tags are Paired tags. So, both have </head> and </title> ending tags respectively.
  5. Thirdly, the <body> tag is used to define the webpage body. All the contents to show on the website are written here.
  6. <h1> tag used to add heading here and also adding the inline CSS here.
  7. Create a <form> with the action of the php file with the method post.
  8. <input> tag used to enter a file path.
  9. Another <input> with the type ‘submit’.
  10. Now we close the HTML file with </html> tag.
  11. We write <?php tag to write PHP within it.
  12. Using an if statement with isset() with $_POST with the name of the submit button
  13. $file_path to add the file path with $_POST method.
  14. 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.
  15. ?> 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.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪