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

标签 php codeigniter unlink

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

notice Undefined index: 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);     
    }
?>

最佳答案

the unlink function shows notice Undefined index: 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: Use this value if you are using an <input> element with the type attribute set to "file".

如果您要使用 CodeIgniter form helper , 你可以使用 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.

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

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

base_url() 辅助函数 returns the URL address网站,which you've setconfig.php文件。

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

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

关于php - 如何在 CodeIgniter 中取消链接(删除)图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21642525/

相关文章:

php - 为什么 Unlink 不适用于此脚本?

php - 删除行时取消链接图像 - pdo

PHP刷新窗口?相当于F5页面重新加载?

javascript - Ajax with twig,它是如何工作的?

mysql - codeigniter 表单验证和 ajax 查找以查看名称是否存在

javascript - Codeigniter 中 ajax 上传的奇怪 MIME 类型

php - Codeigniter:嵌套 View 中的逻辑

PHP unlink 正在请求一个字符串并试图给它 sql 字符串

php - 逆 htmlentities/html_entity_decode

php - 如何动态创建带有指定编号的图像?