php CodeIgniter:如何从文件夹中删除上传的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29972186/
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-26 00:41:14  来源:igfitidea点击:

CodeIgniter : How to delete uploaded image from folder

phpimagecodeigniter

提问by TableMan

I want to delete the image not only in database, but in folder too.

我不仅要删除数据库中的图像,还要删除文件夹中的图像。

This is my database table:

这是我的数据库表:

group_id
group_name
group_descrtion
group_picture

This is my controller :

这是我的控制器:

    public function delete_group()
    {
            $group_id = $this->input->post('group_id');
            $group_picture = $this->input->post('group_picture');

            $this->group_model->delete_group($group_id, $group_picture);
            redirect('product_group');
    }

this is my model :

这是我的模型:

        function delete_group($group_id, $group_picture)
        {
                $this->db->where('group_id', $group_id);

                unlink(base_url("uploads/".$group_picture));

                $this->db->delete('product_group', array('group_id' => $group_id));
        }

and this is my view :

这是我的观点:

 <?=form_open_multipart('product_group/delete_group');?>

   <input type="hidden" class="form-control" placeholder="Group ID" name="group_id">
   <input type="text" class="form-control" placeholder="Group ID" name="group_picture">

  <div class="modal-footer">
     <button data-dismiss="modal" class="btn btn-default" type="button">Cancel</button>
     <button class="btn btn-success" type="submit">Submit</button>
   </div>
 </form>

I succeed in deleting the data in the database, but the image in the folder are not also be deleted.

我成功删除了数据库中的数据,但文件夹中的图像也没有被删除。

Thanks for your help :)

谢谢你的帮助 :)

回答by Jeremy Hymanson

unlink(base_url("uploads/".$group_picture));

Should be

应该

unlink("uploads/".$group_picture);

You need the path, not the url.

您需要路径,而不是网址。

回答by Tpojka

Use CI constant FCPATHas location of root directory, so in your case that should be:

使用 CI 常量FCPATH作为根目录的位置,所以在你的情况下应该是:

unlink( FCPATH . "uploads/" . $group_picture );

Notice that FCPATHalready has trailing slash.

请注意,FCPATH已经有尾随斜线。

回答by Shahzaib Ahsan

USE FCPATH in you Delete function

在您的删除功能中使用 FCPATH

unlink(FCPATH."uploads/".$image);

回答by Khairul Islam

To delete a file/picture you can use delete_files()function. First load the file helper and then pass the file path as a first parameter in the delete_files()function and trueas the second.like-

要删除文件/图片,您可以使用delete_files()功能。首先加载文件助手,然后将文件路径作为delete_files()函数中的第一个参数和第二个参数传递true。like-

delete_files($path, true);

回答by Ijaz Ali

You just need to give only directoryname that starts from the folder, not from base_url

您只需要提供从文件夹开始的目录名,而不是从base_url

unlink("uploads/group/Images/".$group_picture);

unlink("上传/组/图片/".$group_picture);