-1

I am trying to delete an image using a button.

when i click the button i use onClick"'.delete_files(assets/uploads/.$image->filename).'"

But nothing is happening. The image is still where it was before I clicked the button. When I do a print_r($image->filename) I get the 3 names of my images shown on the page.

this is my code:

<table class="images">
    <tr>
    <br/>
    <? foreach($images as $image) { ?>
    <td>
    <a href="assets/uploads/<?= $image->filename ?>" rel="lightbox" title="<?= $image->filename; ?>">
    <img src="<?php echo base_url();?>assets/uploads/<?= $image->filename; ?>" width="200px">
    </a>
<?= '<input type="button" value="Delete" onclick"'.delete_files('assets/uploads/'.$image->filename).'"><br/>'; ?>
    </td>
    <? } ?>
    </tr>
</table>

I wonder if the path is right. but the assets folder is in my root. httpdocs/assets/uploads and my codeigniter folder is at httpdocs/application/ so that should work.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kees Sonnema
  • 5,759
  • 6
  • 51
  • 106
  • You are trying to call a PHP function from the client-side. A client-side action cannot trigger a server-side action without passing data back to the server. – radicalpi May 22 '13 at 13:01
  • So what is the right way of doing this? I used this piece of code from a blog I found, but it is not right then. – Kees Sonnema May 22 '13 at 13:02
  • you need to issue a $_POST or $_GET request to tell the server to delete it. just make sure to clean the data that is passed because of security reasons.. – reikyoushin May 22 '13 at 13:04
  • so something like if($_GET['delete'] > '') { unlink("dir/".$_GET['delete']); } this is just copied from a blog but i don't know how to put that in a button. – Kees Sonnema May 22 '13 at 13:07
  • If you put `if($_GET['delete']) { unlink("dir/".$_GET['delete']); }` on a live server, expect very bad times a la `http://yourURL.com/?delete=../../` https://www.owasp.org/index.php/How_to_write_insecure_code – stormdrain May 22 '13 at 13:31

2 Answers2

0

I am pretty sure that you are calling php function on onclick attribute of the button which is wrong you cannot call directly server side functions on the html element's attribute

While using codeigniter make a function in your controller and place the anchor tag instead of button like

<table class="images">
<tr>
<br/>
<? foreach($images as $image) { ?>
<td>
<a href="assets/uploads/<?= $image->filename ?>" rel="lightbox" title="<?= $image->filename; ?>">
<img src="<?php echo base_url();?>assets/uploads/<?= $image->filename; ?>" width="200px">
</a>

<?= '<a  title="Delete" href="'.site_url().'controller/delete_files/'.$image->filename).'"><br/>'; ?>
</td>
<? } ?>
</tr>
</table>

In your controller make a function delete_files()

 function delete_files($image_name){     
 // write the code to delete or unlink $image_name
 unlink('ROOT PATH'.$image_name);
 }
M Khalid Junaid
  • 63,861
  • 10
  • 90
  • 118
0

use

unlink('./path/to/directory/');
semira
  • 341
  • 3
  • 12