gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 12:22:02 24 4
gpt4 key购买 nike

我尝试在 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com