I want to to destroy all images within a folder with PHP how can I do this?
You can delete files using the Python os. remove(), os. rmdir(), and shutil. rmtree() method.
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"); ?>
'" 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 '.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With