0

Long time I’ve been trying to delete an image from the database as well as the file path. There's no problem uploading the images. My controller:

 public function deleteFile()  {
if ($this->images_model->delete_file($user_id, $fileName))  {            
         $status = 'success';
         $msg = 'File successfully deleted';
    }else{
    $status = 'error';
    $msg = 'Something went wrong when deleting the file, please try again';
    }
   echo json_encode(array('status' => $status, 'msg' => $msg)); 

The Images_model is the following:

public function delete_file ($user_id, $fileName)  {        
    $this->db->where('user_id', $user_id);
    unlink("uploads/".$fileName);
    $this->db->delete('images', array('user_id' => $user_id));

}

My Table looks something like this attached IMAGE:

The problem might lie, I think, with my code for executing the delete. Here is the view where I’m trying to do something like the following. ‘main’ is pointing to the controller:

<input type="image" id="delete-profile-img" src="<?= base_url('/assets/images/delete-icon-gray.png') ?>" title="Delete Image" onclick="'$this->main->deleteFile'" >

Is it OK to use “onclick” while pointing to the function or do I need to follow that up with more complex java or ajax? Thanks in advance for any and all input.

dreamerdan
  • 43
  • 7

2 Answers2

0

You are mixing and confusing JavaScript events with Codeigniter code in a way that wont work. You need to pass an identifier to the file you want to delete. I suggest you allow your controller to receive a parameter for the file you want to delete:

public function deleteFile($fileName)  { // SET THE FILE NAME AS PARAMETER
// Make sure to also set the variable $user_id here in some appropriate way 

  if ($this->images_model->delete_file($user_id, $fileName))  {            
      $status = 'success';
      $msg = 'File successfully deleted';
  }else{
      $status = 'error';
      $msg = 'Something went wrong when deleting the file, please try again';
  }
  echo json_encode(array('status' => $status, 'msg' => $msg)); 
}

And the link to delete a file can be a simple link with the file name as parameter that you can specify on page rendering:

<a href="/deleteFile/<?=$filename?>">Delete</a>

But i guess from your code that you want some AJAX-handling through a GET or POST request to handle the JSON response. How you set this up depends on which JavaScript library or other approach you use. You need to write a JavaScript function to handle the request. The link could be something like:

<input type="image" id="delete-profile-img" src="<?= base_url('/assets/images/delete-icon-gray.png') ?>" title="Delete Image" onclick="delete(<?=$fielname?>)" >

And the JavaScript function could look something like this if you use Jquery:

function delete(filename){
   var url = '/deleteFile/'+ filename;
   $.get(url, function(obj) {
      //console.log(obj);
      // DO SOMETHING WITH THE RESPONSE HERE... 
    })
}
Michael Krikorev
  • 2,126
  • 1
  • 18
  • 25
  • Sorry I've been a bit tied up but Im still looking into your suggestions. Only thing is I don't have a separate delete file. I will try to reference the function. – dreamerdan Jan 22 '19 at 16:31
  • Also Im not too familiar with JS. Could you please tell me what you mean by //do something with the response//. It looks like that might work. Will let you know. Thank! – dreamerdan Jan 22 '19 at 16:35
  • It's working but it's deleting ALL the images for the respected user. I'm also getting an error that the unlink statement path does not exist when I know it does. The Model: public function deleteFile ($user_id, $fileName) { $this->db->where('user_id', $user_id); unlink('uploads/'.$fileName); $this->db->delete('images', array('user_id' => $user_id)); Will see if I can figure out the problem. Thanks. – dreamerdan Jan 22 '19 at 21:32
  • To "do something with the response" is for example if you want to alert the user of the deleted file with Javascript. You have to change the model to only delete the file, not all files for the user. Something like: `$this->db->where( array('user_id' => $user_id, 'filename' => $fileName));` Translated to SQL: `DELETE FROM table WHERE user_id = ? AND filename = ?` – Michael Krikorev Jan 24 '19 at 14:33
  • I posted another related question about deleting the image. It's still deleting ALL images. I'm having a similar problem with copying the image to another path. This tells me I have to find a way to assign a variable to the user's selection. If you see the new question you should get information. Thanks again! – dreamerdan Jan 24 '19 at 19:47
  • I was able to fix it. I posted an answer on it so hopefully someone else might benefit from it. – dreamerdan Jan 25 '19 at 00:32
0

After watching a video on this, I found the missing link I needed to get my image unlink and delete to work very nicely. Since the images were already being uploaded, I used the same path and image name to add to the delete button in the VIEW:

<a id="delete-profile-img" class="btn btn-danger btn-xs" href="<?php echo base_url().'main/deleteFile/'."uploads/".$images->imageName ?>" title="Delete">Delete</a>

I then added some additional code to the CONTROLLER:

public function deleteFile($images, $imageName)  {                   
         $user_id = $this->session->userdata['user_id'];             
                $result = $this->db->get_where('images', array('user_id' => $user_id, 'imageName' => $imageName));         
         $rows = $result->result(); 
         foreach ($rows as $row) {
         unlink("uploads/".$imageName);             
         }             
         $this->images_model->deleteFile($user_id, $imageName); 
         redirect(site_url().'main/index');                         
      }

The MODEL:

public function deleteFile ($user_id, $imageName)  {              
    return $this->db->delete('images', array('user_id' => $user_id, 'imageName' => $imageName));        
    }   

Thanks to those who provided valuable feedback on this topic. Hopefully this might help others who are trying to do the same.

dreamerdan
  • 43
  • 7