【问题标题】:codeigniter file upload with other elementscodeigniter 文件上传与其他元素
【发布时间】:2013-04-12 06:06:42
【问题描述】:

我有一个带有表单的页面。表单上有一个下拉列表和一些输入框和一个文件上传。如果我尝试提交我的页面,我会得到一个空白页面,并显示错误“不存在的类:上传”。我还没有使用文件上传。

这是我的输入文件类型:

<tr><td>Image: </td></tr>
<tr><td><input type="file" name="image" size="20" /></td></tr>

我在 CI 文件上传类中看到你需要一个 form_open_multipart('upload');那么我将只拥有该表格还是必须在我的情况下做其他事情?因为我在该表单上有其他输入类型?

这是我的控制器的一点代码:

$subject = htmlspecialchars(trim($this->input->post('subject')));
$message = htmlspecialchars(trim($this->input->post('message')));
$image = $this->input->post('image');

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);

if (!$this->upload->do_upload($image)) {          
    $data['fout'] = $this->upload->display_errors();
    $this->annuleren($soort, $data);
} else {
    $data['upload'] =  $this->upload->data();
}

$id = $this->message_model->insert($message, $subject, $image);

redirect('infoscreen/dashboard', 'refresh');

我还在“源文件”下创建了一个“上传”文件夹。我可能在做一些愚蠢的事情。有人可以帮助我并告诉我我做错了什么以及如何解决它吗?

非常感谢:)

【问题讨论】:

    标签: image forms codeigniter file-upload


    【解决方案1】:

    编辑:首先加载上传库:

    $this->load->library('upload');
    

    另一个问题在于您引用了错误的文件。

    这一行是不必要的:

    $image = $this->input->post('image'); // <- remove this line
    

    只需使用:

    if (!$this->upload->do_upload('image')) {   
    

    CodeIgniter 默认查找名为“userfile”的文件输入,但如果不存在,您需要将 'image' 添加到 do_upload() 方法中,如下所示。

    然后你有一行:

    $id = $this->message_model->insert($message, $subject, $image);
    

    我不确定您认为$image 变量中应该包含什么,但如果上传成功,您可以附加来自$data['upload'] 的数据:

    $id = $this->message_model->insert($message, $subject, $data['upload'][file_name]); // <- gets the filename 
    

    完整参考:http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

    还要回答您的第二个问题,多部分表单类型具有其他输入字段/类型不是问题。

    【讨论】:

    • 哦,好的,但我仍然收到该错误。将用户文件添加到 do_upload 是什么意思?
    • 啊,是的,您需要$this-&gt;load-&gt;library('upload'); 看到我的编辑。
    • 即使我注释掉 ->do_upload 代码等,它仍然会给出同样的错误。
    • 好的。检查您是否有错误报告(在您的配置文件中),然后在 application/logs/ 中查看日志
    • 你有没有机会在文件夹 system/libraries 中没有文件 Upload.php ???
    猜你喜欢
    • 2014-01-14
    • 2015-01-27
    • 2017-03-16
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 2020-03-02
    • 2023-03-30
    • 1970-01-01
    相关资源
    最近更新 更多