如何在 CodeIgniter 中取消链接(删除)图像

问题描述 投票:0回答:12

我尝试在 CodeIgniter 中

unlink
图像,但
unlink
函数显示:

注意未定义索引:userfile

这是我的代码

<?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);     
    }
?>
php codeigniter unlink
12个回答
4
投票

取消链接功能显示通知未定义索引:

userfile

上传表单,使用multipart/form-data

确保您已在上传表单中使用

enctype="multipart/form-data"
属性/值。

<form action="" method="post" accept-charset="utf-8" enctype="multipart/form-data">

来自 MDN

enctype

multipart/form-data
:如果您使用
<input>
,请使用此值 type 属性设置为“file”的元素。

如果您要使用 CodeIgniter form helper,您可以使用

form_open_multipart()
函数:

此功能与上面的

form_open()
标签完全相同 除了它添加了一个多部分属性,如果您 想要使用表单来上传文件。

删除文件,文件路径与 URL 地址

PHP

unlink()
函数接受文件的 Path 作为第一个参数。不是 URL 地址

base_url()
辅助函数返回站点的URL地址您在config.php
文件中设置

您必须使用服务器上文件的路径,如下所示:

unlink('/path/to/image/image_name.jpg'); // This is an absolute path to the file

您可以使用 AbsoluteRelative 路径,但请注意,相对路径 是相对于 index.php

 文件的。即,如果 
image/
 文件夹位于 
index.php
 文件旁边,则应使用 
image/image_name.jpg
 作为文件路径:

unlink('image/image_name.jpg'); // This is a relative path to the file
    

2
投票
如果您想上传用户个人资料照片并且还 此时应删除旧用户照片 在 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'); }
    

0
投票
首先检查您的文件输入名称。它是“用户文件”吗? 如果没有,则添加它,然后再次运行。


0
投票
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'; } }
    

0
投票
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."));


    


0
投票

第一个取消链接函数仅适用于路径(没有url),因此请删除base_url()函数。

然后在你的第一个函数中

$_FILES数组不包含userfile索引,这就是为什么会出现错误。

注意:- 在使用 unlink 之前,我还想使用 file_exists() 函数,首先它将检查文件是否存在于同一路径上,然后使用 unlink 函数(用于错误处理)。

就像那样 -

<?php if(file_exists(filePath)) { unlink(filePath); } ?>

请解决这两个问题。

谢谢


0
投票
假设数据库中有该文件名,并且您的文件是 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 文件上传部分中使用的路径相同。即

... $config['upload_path'] = './assets/uploads/files'; ...

因此我们在上传和取消链接部分都使用了相对路径。因此,如果上传工作正常,那么这也将工作正常。


0
投票
您可以这样做,您需要首先获取上传图像的目录路径。 示例:

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

0
投票
# CodeIgniter 3 # These methods will give you the document root path in CodeIgniter 3. You can use this path to construct URLs, file paths, or for any other purposes that require the document root. public function delete() { $id = $this->input->get('id'); if(!empty($id)) { $this->db->select("pdf_link"); $this->db->from("ad_study"); $this->db->where("st_id",$id); $query = $this->db->get(); $data = $query->row_object(); // $file = base_url('uploads/'.$data->pdf_link); $file = FCPATH.'/uploads/'.$data->pdf_link; if (file_exists($file)) { if (unlink($file)) { echo "File deleted successfully."; } else { echo "Unable to delete the file."; } } else { echo "File does not exist."; } // die; $this->db->where('st_id',$id); $this->db->delete("ad_study"); $this->session->set_flashdata('db_success','Success - Data Delete Successfully '); redirect('admin/material'); } }
    

-1
投票
$this->load->helper("file");

只需在取消链接之前添加上面的行

unlink($path);
    

-1
投票
您可以使用此代码删除文件夹中的图像吗?

unlink(realpath('上传/' . $_image));

“uploads”:图像存在于上传文件夹中。 "$_image" : 图片文件名

PHP函数: 1:取消链接() 2:真实路径()

上述代码在我的网站上成功运行以删除文件夹中的图像。


-2
投票
试试这个代码

unlink(base_url().'/image/logo_thumb'.$logo_thumb);

注意:您没有在

$logo_thumb

 中分配/声明 
deleteconf()

请检查您的代码。

© www.soinside.com 2019 - 2024. All rights reserved.