[英]How to unlink image and delete from database using codeigniter in php
[英]How to unlink (delete) an image in CodeIgniter
我尝试在 CodeIgniter 中unlink
图像,但unlink
功能显示:
通知未定义索引:用户文件
这是我的代码
<?php
function get_from_post(){
$data['logo_name'] = $this->input->post('logo_name',TRUE);
$data['logo_thumb'] = $_FILES['userfile']['name'];
return $data;
}
function deleteconf($data){
$data= $this->get_from_post();
$update_id=$this->uri->segment(3);
@unlink(base_url.'image/logo_thumb'.$logo_thumb);
$query= $this->_delete($update_id);
}
?>
unlink 函数显示通知 Undefined index:
userfile
确保您已为上传表单使用enctype="multipart/form-data"
属性/值。
<form action="" method="post" accept-charset="utf-8" enctype="multipart/form-data">
来自MDN :
加密类型
multipart/form-data
:如果您使用 type 属性设置为“file”的<input>
元素,请使用此值。
如果你打算使用 CodeIgniter 表单助手,你可以使用form_open_multipart()
函数:
此函数与上面的
form_open()
标记完全相同,只是它添加了一个 multipart 属性,如果您想使用表单上传文件,这是必需的。
PHP unlink()
函数接受文件的路径作为第一个参数。 不是URL 地址。
base_url()
辅助函数返回您在config.php
文件中设置的站点的 URL 地址。
您必须使用服务器上文件的路径,如下所示:
unlink('/path/to/image/image_name.jpg'); // This is an absolute path to the file
您可以使用绝对路径或相对路径,但请注意相对路径是相对于index.php
文件的。 即如果image/
文件夹放在index.php
文件旁边,你应该使用image/image_name.jpg
作为文件路径:
unlink('image/image_name.jpg'); // This is a relative path to the file
如果您想为用户个人资料上传照片,并且同时应该使用 codeignitor 中的 unlink 方法删除旧的用户照片
$query = $this->db->where('profile_id',$profile_id)->get('users')->row();
$result = $query->photo;
$result = http://localhost/abc/user_photo/FE12563.jpg
if ($result) {
$dd = substr($result, strlen(base_url()));
unlink($dd);
return $this->db->set('photo',$photo)->where('profile_id',$profile_id)->update('users');
}
function get_from_post(){
$filename = $_POST['logo_name'];
$path = $_SERVER['DOCUMENT_ROOT'].'/projectname/uploads/'.$filename ;
if(is_file($path)){
unlink($path);
echo 'File '.$filename.' has been deleted';
} else {
echo 'Could not delete '.$filename.', file does not exist';
}
}
首先检查您的文件输入名称。 它是“用户文件”吗? 如果没有,则添加它,然后再次运行它。
base_url() 函数返回您项目的 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."));
第一个取消链接功能仅适用于路径(无 url),因此请删除 base_url() 功能。
然后在你的第一个函数$_FILES数组中不包含用户文件索引,这就是出错的原因。
注意:- 在使用 unlink 之前,我还想使用 file_exists() 函数,首先它会检查文件是否存在于同一路径上,然后使用 unlink 函数(用于错误处理)。
像那样 -
<?php
if(file_exists(filePath))
{
unlink(filePath);
}
?>
请解决这两个问题。
谢谢
假设您在数据库中有文件名并且您的文件是 abc.jpg。 然后在 Code Igniter 中取消链接,只需执行 -
$file = "abc.jpg";
$prev_file_path = "./assets/uploads/files/".$file;
if(file_exists($prev_file_path ))
unlink($prev_file_path );
我的文件上传路径是“public_html/assets/uploads/files/”。 我正在我的控制器中编写上述代码,即“public_html/application/controllers/MyController.php”。 所以路径将与我在 CI 文件上传部分中使用的路径相同。 IE
...
$config['upload_path'] = './assets/uploads/files';
...
所以我们在上传和取消链接部分都使用了相对路径。 因此,如果上传工作完美,那么这也将完美工作。
您可以这样做,您需要首先获取上传图像的目录路径。 示例:
//this is your url
$mainPath='https://google.com/uploads/image/16.jpeg';
$uploadedDirectory=str_replace("https://google.com/","./", $mainPath);
//now you get path like this ./uploads/image/16.jpeg
if(unlink($uploadedDirectory)) {
$this->db->where('prodId',$id);
$this->db->delete('products');
}
$this->load->helper("file");
只需在取消链接之前添加以上行
unlink($path);
您能否使用此代码删除文件夹上的图像。
unlink(realpath('uploads/' . $_image));
"uploads" : 图片存在于上传文件夹中。 "$_image" : 图片文件名
PHP 函数: 1:unlink() 2:realpath()
上面的代码在我的网站上成功地删除了文件夹上的图像。
试试这个代码
unlink(base_url().'/image/logo_thumb'.$logo_thumb);
注意:您没有在deleteconf()
中分配/声明$logo_thumb
。
请检查您的代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.