Tags
Asked 2 years ago
30 Jun 2021
Views 223
Scarlett

Scarlett posted

delete image from folder PHP

delete image from folder PHP
lain

lain
answered May 4 '23 00:00

To delete an image from a folder in PHP, we can use the unlink() function that is built into PHP. Here's an example of how we can delete an image:


$filename = "path/to/image.jpg";
if (file_exists($filename)) {
    if (unlink($filename)) {
        echo "Image deleted successfully.";
    } else {
        echo "Error deleting image.";
    }
} else {
    echo "Image not found.";
}

In the example above, we first check if the image file exists using the file_exists() function. If the file exists, we then use the unlink() function to delete the file . If the deletion is successful, we output a success message. Otherwise, we output an error message.

It's important to note that the unlink() function permanently deletes the file, so make sure you have a backup of the file if you need to keep it.

Post Answer