从 Codeigniter 上传后如何删除文件和图像?
How Delete File and Image after upload from Codeigniter?
这是我的代码控制器"contract.php"
function delete($con_id){
//$year=$this->session->userdata('year');
$path = ('/assets/upload/employees/contracts/');
$get_file = $path.$con_id.'.jpg';
$this->db->where('con_id',$con_id);
$this->db->delete('sch_emp_contract');
if(file_exists($get_file)){
unlink(base_url("/assets/upload/employees/contracts/'.$con_id."));
}
$m='';
$p='';
if(isset($_GET['m'])){
$m=$_GET['m'];
}
if(isset($_GET['p'])){
$p=$_GET['p'];
}
redirect("employee/contract?m=$m&p=$p");
}
此代码查看contract_list.php
- 按钮删除
<td width="1%" class="remove_tag">';
if($this->green->gAction("D")){
$tr .='<a title="Delete Contract" id="clk_del" class="clk_del">
<img rel="'.$contract['con_id'].'" src="'.site_url('../assets/images/icons/delete.png').'" onclick="delete_contrac (event);" style="width:20px;height:20px;"></a>';}$tr .='</td>
函数
function delete_contract(event){
var r = confirm("Are you sure to delete this record !");
if( r == true){
var contr_id= $(event.target).attr('rel');
location.href="<?PHP echo site_url('employee/contract/delete');?>/"+contr_id+"?<?php echo "m=$m&p=$p" ?>";
}
}
成功从数据库中删除,但无法删除文件夹文件和图片上传。
检查您的 file_exists
returns 是对还是错。然后尝试这样的事情。
$path = BASEPATH.'/assets/upload/employees/contracts/';//get absolute path
$get_file = $path.$con_id.'.jpg';
$this->db->where('con_id',$con_id);
$this->db->delete('sch_emp_contract');
if(file_exists($get_file)){
unlink($get_file);
}
base_url()
函数 returns url
你的项目但是在这里你必须使用你想要删除的文件的目录路径。
$path = BASEPATH.'/assets/upload/employees/contracts/';
$get_file = $path.$con_id.'.jpg';
if(file_exists($get_file)){
unlink($get_file);
}
而不是
unlink(base_url("/assets/upload/employees/contracts/'.$con_id."));
这是我的代码控制器"contract.php"
function delete($con_id){
//$year=$this->session->userdata('year');
$path = ('/assets/upload/employees/contracts/');
$get_file = $path.$con_id.'.jpg';
$this->db->where('con_id',$con_id);
$this->db->delete('sch_emp_contract');
if(file_exists($get_file)){
unlink(base_url("/assets/upload/employees/contracts/'.$con_id."));
}
$m='';
$p='';
if(isset($_GET['m'])){
$m=$_GET['m'];
}
if(isset($_GET['p'])){
$p=$_GET['p'];
}
redirect("employee/contract?m=$m&p=$p");
}
此代码查看contract_list.php - 按钮删除
<td width="1%" class="remove_tag">';
if($this->green->gAction("D")){
$tr .='<a title="Delete Contract" id="clk_del" class="clk_del">
<img rel="'.$contract['con_id'].'" src="'.site_url('../assets/images/icons/delete.png').'" onclick="delete_contrac (event);" style="width:20px;height:20px;"></a>';}$tr .='</td>
函数
function delete_contract(event){
var r = confirm("Are you sure to delete this record !");
if( r == true){
var contr_id= $(event.target).attr('rel');
location.href="<?PHP echo site_url('employee/contract/delete');?>/"+contr_id+"?<?php echo "m=$m&p=$p" ?>";
}
}
成功从数据库中删除,但无法删除文件夹文件和图片上传。
检查您的 file_exists
returns 是对还是错。然后尝试这样的事情。
$path = BASEPATH.'/assets/upload/employees/contracts/';//get absolute path
$get_file = $path.$con_id.'.jpg';
$this->db->where('con_id',$con_id);
$this->db->delete('sch_emp_contract');
if(file_exists($get_file)){
unlink($get_file);
}
base_url()
函数 returns url
你的项目但是在这里你必须使用你想要删除的文件的目录路径。
$path = BASEPATH.'/assets/upload/employees/contracts/';
$get_file = $path.$con_id.'.jpg';
if(file_exists($get_file)){
unlink($get_file);
}
而不是
unlink(base_url("/assets/upload/employees/contracts/'.$con_id."));