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

2024-06-19

我试着unlinkCodeIgniter 中的图像,但是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);     
    }
?>

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

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

确保您已经使用过enctype="multipart/form-data"您的上传表单的属性/值。

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

来自MDN https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#attr-enctype:

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

如果您要使用 CodeIgniter表单助手 http://ellislab.com/codeigniter/user-guide/helpers/form_helper.html,你可以使用form_open_multipart()功能:

该功能与form_open()上面的标签 除了它添加了一个多部分属性,如果您 想要使用表单来上传文件。

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

PHP unlink() https://www.php.net/unlink函数接受Path文件的作为第一个参数。不是网址地址.

The base_url() https://github.com/EllisLab/CodeIgniter/blob/2.1-stable/system/helpers/url_helper.php#L64-L68辅助函数返回 URL 地址 https://github.com/EllisLab/CodeIgniter/blob/2.1-stable/system/core/Config.php#L282该网站的,你设置的 https://github.com/EllisLab/CodeIgniter/blob/2.1-stable/application/config/config.php#L17 in the config.php file.

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

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

你可以使用Absolute or Relative路径,但请注意相对路径 是相对于 the index.php文件。即如果image/文件夹放在旁边index.php文件,你应该使用image/image_name.jpg作为文件路径:

unlink('image/image_name.jpg'); // This is a relative path to the file
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 CodeIgniter 中取消链接(删除)图像 的相关文章

随机推荐