如何取消链接(删除)CodeIgniter中的图像 [英] How to unlink (delete) an image in CodeIgniter

查看:145
本文介绍了如何取消链接(删除)CodeIgniter中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 unlink 一个图像在CodeIgniter,但 unlink 函数显示:



p>

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


解决方案


通知未定义的索引: userfile




数据



请确保您已使用 enctype =multipart / form-data属性/值

 < form action =method =postaccept-charset =utf-8enctype = multipart / form-data> 

MDN


enctype

multipart / form-data :如果使用< input&


如果你要去要使用CodeIgniter 表单助手,您可以使用 form_open_multipart()函数:


此函数与 标记在
之上,除了它添加了一个multipart属性,如果
想要使用该表单来上传文件,那么这是必要的。




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



PHP unlink() 函数接受文件的路径不是网址



base_url() 助手函数返回网站的网址您在配置中已设置



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

  unlink('/ path / to / image / image_name.jpg'); //这是文件的绝对路径

您可以使用绝对相对路径,但请注意相对路径 是相对于 index.php 文件。即如果 image / 文件夹放在 index.php 文件中,则应使用 image /image_name.jpg 作为文件路径:

  image / image_name.jpg'); //这是文件的相对路径


I try to unlink an image in CodeIgniter, but the unlink function shows:

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

解决方案

the unlink function shows notice Undefined index: userfile

The upload form, using multipart/form-data

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

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

From the MDN:

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

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

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.

Deleting files, File path vs URL address

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

The base_url() helper function returns the URL address of the site, which you've set in the config.php file.

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 Absolute or Relative path, but note that the relative path is relative to the index.php file. i.e. If the image/ folder is placed beside index.php file, you should use image/image_name.jpg as the file path:

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

这篇关于如何取消链接(删除)CodeIgniter中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆