Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

delete image from folder PHP

Tags:

php

I have a folder where I keep my images, named img/. I have a table with all of my images:

<table border="3">     <tr>         <td>                 <?php             $files = glob("img/*");             foreach ($files as $file) {                 echo "<div class='divimages'>";                  echo '<img src="'.$file.'"/>';                 echo "<input type='submit' value='Delete image'/><br>";                 echo "</div>";               }             ?>         </td>     </tr>    </table> 

How can I delete the image associated to the button with the value:"Delete image".

like image 245
emcee22 Avatar asked Feb 21 '13 15:02

emcee22


People also ask

How can delete image from database and folder in PHP?

You can use unlink() function to delete the image from directory. If you image is the same directory of script and filename is in variable $id you can delete it by using unlink($id) . Here path/ is the location of the directory in which image is stored. Save this answer.

How can I delete upload file in PHP?

In PHP, we can delete any file using unlink() function. The unlink() function accepts one argument only: file name. It is similar to UNIX C unlink() function. PHP unlink() generates E_WARNING level error if file is not deleted.

How do I delete a file in HTML?

Right-click the files again and choose "Delete." Click "Yes" to confirm the deletion. Repeat this process to delete all the HTML read-only files on the computer.


2 Answers

There are a few routes. One, the most simple, would involve making that into a form; when it submits you react to POST data and delete the image using unlink

DISCLAIMER: This is not secure. An attacker could use this code to delete any file on your server. You must expand on this demonstration code to add some measure of security, otherwise you can expect bad things.

Each image's display markup would contain a form something like this:

echo '<form method="post">';   echo '<input type="hidden" value="'.$file.'" 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 '.$filename.' has been deleted';   } else {     echo 'Could not delete '.$filename.', file does not exist';   } } // existing code continues below... 

You can elaborate on this by using javascript: instead of submitting the form, you could send an AJAX request. The server-side code would look rather similar to this.

Documentation and Related Reading

  • unlink - http://php.net/manual/en/function.unlink.php
  • $_POST - http://php.net/manual/en/reserved.variables.post.php
  • file_exists - http://php.net/manual/en/function.file-exists.php
  • array_key_exists - http://php.net/manual/en/function.array-key-exists.php
  • "Using PHP With HTML Forms" - http://www.tizag.com/phpT/forms.php
like image 71
Chris Baker Avatar answered Sep 24 '22 07:09

Chris Baker


You can delete files in PHP using the unlink() function.

unlink('path/to/file.jpg'); 
like image 26
mcryan Avatar answered Sep 24 '22 07:09

mcryan