删除图像以及文件路径

时间:2019-01-19 22:21:46

标签: mysql codeigniter delete-file

很长时间以来,我一直试图从数据库中删除图像以及文件路径。上传图片没有问题。我的控制器:

 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)); 

Images_model如下:

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:

我认为问题可能在于执行删除操作的代码。这是我尝试执行以下操作的视图。 “主”指向控制器:

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

在指向函数时可以使用“ onclick”吗?还是我需要使用更复杂的java或ajax来进行跟踪?预先感谢您提供的所有信息。

2 个答案:

答案 0 :(得分:0)

您正在以无法正常工作的方式将JavaScript事件与Codeigniter代码混合和混淆。您需要将标识符传递到要删除的文件。我建议您允许控制器接收要删除的文件的参数:

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)); 
}

删除文件的链接可以是一个简单的链接,其文件名作为参数,您可以在页面渲染中指定该参数:​​

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

但是我从您的代码中猜测,您希望通过GET或POST请求进行一些AJAX处理来处理JSON响应。设置方式取决于您使用哪种JavaScript库或其他方法。您需要编写一个JavaScript函数来处理请求。链接可能类似​​于:

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

如果您使用Jquery,则JavaScript函数可能类似于以下内容:

function delete(filename){
   var url = '/deleteFile/'+ filename;
   $.get(url, function(obj) {
      //console.log(obj);
      // DO SOMETHING WITH THE RESPONSE HERE... 
    })
}

答案 1 :(得分:0)

在观看了有关此视频的视频后,我发现丢失了链接,我需要取消链接才能删除图像并删除它,以使其正常工作。由于图像已经上传,我使用相同的路径和图像名称将其添加到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>

然后我向控制器添加了一些其他代码:

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');                         
      }

模型:

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

感谢那些对此主题提供了宝贵反馈的人。希望这可以帮助其他尝试这样做的人。