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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21642525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:18:45  来源:igfitidea点击:

How to unlink (delete) an image in CodeIgniter

phpcodeigniterunlink

提问by Arul

I try to unlinkan image in CodeIgniter, but the unlinkfunction shows:

我尝试unlink在 CodeIgniter 中创建图像,但该unlink函数显示:

notice Undefined index: userfile

通知未定义索引:用户文件

Here is my code

这是我的代码

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

回答by Hashem Qolami

the unlink function shows notice Undefined index: userfile

unlink 函数显示通知未定义索引: userfile

The upload form, using multipart/form-data

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

Make sure you've use enctype="multipart/form-data"attribute/value for your upload form.

确保您已将enctype="multipart/form-data"属性/值用于上传表单。

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

From the MDN:

来自MDN

enctype
multipart/form-data: Use this value if you are using an <input>element with the type attribute set to "file".

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

If you're going to use CodeIgniter form helper, you could use form_open_multipart()function:

如果你打算使用 CodeIgniter表单助手,你可以使用form_open_multipart()函数:

This function is absolutely identical to the form_open()tag above except that it adds a multipart attribute, which is necessary if you would like to use the form to upload files with.

这个函数与form_open()上面的标签完全相同,只是它添加了一个 multipart 属性,如果你想使用表单上传文件,这是必要的。

Deleting files, File path vs URL address

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

PHP unlink()function accepts the Pathof the file as the first argument. Not the URL Address.

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

The base_url()helper function returns the URL addressof the site, which you've setin the config.phpfile.

base_url()辅助函数返回的URL地址在现场,你已经设置了在config.php文件中。

You'll have to use the path of the file on your server, as follows:

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

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

You could use an Absoluteor Relativepath, but note that the relative pathis relative tothe index.phpfile. i.e. If the image/folder is placed beside index.phpfile, you should use image/image_name.jpgas the file path:

你可以使用一个绝对相对路径,但要注意的是,相对路径是相对于index.php文件。即如果image/文件夹放在index.php文件旁边,你应该使用image/image_name.jpg作为文件路径:

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

回答by Rakesh Maurya

If you want to upload a photo for user profile and also at the time the old user photo should be deleted by using unlink method in codeignitor

如果您想上传用户个人资料的照片,并且同时应使用 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');
}

回答by Reena Shirale

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

回答by Snehal S

First check your file input name. Is it "userfile" or not? if not then add it and then run it once again.

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

回答by Syed Ali

base_url() function returns url of you project but here you have to use directory path of file which you want to delete.

base_url() 函数返回您项目的 url,但在这里您必须使用要删除的文件的目录路径。

$path = BASEPATH.'/assets/upload/employees/contracts/'; 
$get_file = $path.$con_id.'.jpg'; 
 if(file_exists($get_file)){ 
 unlink($get_file); 
 }


instead of unlink(base_url("/assets/upload/employees/contracts/'.$con_id."));

代替 unlink(base_url("/assets/upload/employees/contracts/'.$con_id."));

回答by Sorav Garg

First unlink function work only with path (without url), so please remove base_url() function.

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

Then in your first function $_FILESarray doesn't contain userfileindex, that's why getting error.

然后在你的第一个功能$ _FILES数组不包含userfile的指数,这就是为什么得到错误。

NOTE:- Before using unlink i would like to use also file_exists() function, first it will check if file is exist on same path then use unlink function (for error handling).

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

Like that -

像那样 -

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

?>

Please fix both issue.

请解决这两个问题。

Thanks

谢谢

回答by Sourav Purkait

Suppose you have the file name in the database and your file is abc.jpg. Then to unlink that in Code Igniter just do -

假设您在数据库中有文件名并且您的文件是 abc.jpg。然后在 Code Igniter 中取消链接,只需执行 -

$file = "abc.jpg";
$prev_file_path = "./assets/uploads/files/".$file;
if(file_exists($prev_file_path ))
    unlink($prev_file_path );

My file upload path is "public_html/assets/uploads/files/". I am writing the above code in my controller which is "public_html/application/controllers/MyController.php". So the path will be same which I used in my CI file upload section. i.e.

我的文件上传路径是“public_html/assets/uploads/files/”。我正在我的控制器中编写上述代码,即“public_html/application/controllers/MyController.php”。所以路径将与我在 CI 文件上传部分中使用的路径相同。IE

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

So we used relative path in both the upload and unlink section. So if the upload works perfectly then this will also work perfectly.

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

回答by Mayur Solanki

Can you please use this code to remove image on folder.

您能否使用此代码删除文件夹上的图像。

unlink(realpath('uploads/' . $_image));

unlink(realpath('uploads/' . $_image));

"uploads" : Image exist in uploads folder. "$_image" : Image file name

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

PHP functions: 1 : unlink() 2 : realpath()

PHP 函数: 1 : unlink() 2 : realpath()

The above code successfully working on my site to delete image on folder.

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

回答by Kiran Patel

$this->load->helper("file");

just add above line before unlink

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

unlink($path);

回答by Kumar V

try this code

试试这个代码

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

Note: You didn't assign / declare $logo_thumbin your deleteconf().

注意:您没有$logo_thumbdeleteconf().

Please check your code.

请检查您的代码。