Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete images from a folder

I want to to destroy all images within a folder with PHP how can I do this?

like image 434
arcus_marcus Avatar asked Apr 04 '11 06:04

arcus_marcus


People also ask

How do I remove an image from a directory in Python?

You can delete files using the Python os. remove(), os. rmdir(), and shutil. rmtree() method.

How can delete image from database and folder in PHP?

php include('../connect. php'); $id=$_GET['id']; $result = $db->prepare("DELETE FROM student WHERE id= :memid"); $result->bindParam(':memid', $id); $result->execute(); header ("location: students. php"); ?>

How do you remove an image from a folder using PHP write the code?

'" name="delete_file" />'; echo '<input type="submit" value="Delete image" />'; echo '</form>'; ...and at at the top of that same PHP file: if (array_key_exists('delete_file', $_POST)) { $filename = $_POST['delete_file']; if (file_exists($filename)) { unlink($filename); echo 'File '.


1 Answers

foreach(glob('/www/images/*.*') as $file)
    if(is_file($file))
        @unlink($file);

glob() returns a list of file matching a wildcard pattern.

unlink() deletes the given file name (and returns if it was successful or not).

The @ before PHP function names forces PHP to suppress function errors.

The wildcard depends on what you want to delete. *.* is for all files, while *.jpg is for jpg files. Note that glob also returns directories, so If you have a directory named images.jpg, it will return it as well, thus causing unlink to fail since it deletes files only.

is_file() ensures you only attempt to delete files.

like image 177
Christian Avatar answered Oct 01 '22 03:10

Christian