Codeigniter 表单通过文件上传提交

Posted

技术标签:

【中文标题】Codeigniter 表单通过文件上传提交【英文标题】:Codeigniter form submit with file upload 【发布时间】:2013-01-08 00:57:05 【问题描述】:

我正在尝试编写一个代码,我可以在其中提交表单,在数据库中输入内容,同时执行文件上传并将其存储在我服务器内的文件夹中

文件夹位置称为uploads,位于我网站的根目录

这是我的代码

控制器(site.php

 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

 class Site extends CI_controller 

public function __construct()

    parent::__construct();
    // Your own constructor code
    $this->load->model("site_model");
    $this->load->helper(array('form', 'url'));
 

public function cmain($type,$page)

    $data = $this->_initialize_data(); //this is just a bunch of variables that im calling in another function
    $data['type'] = $type;
    $data['page'] = $page;
    $this->load->vars($data);
    $this->load->view('site/cmain');



public function m1()
                       

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

            if(isset($_POST['m1']))
            
                $suffix = $this->input->post("suffix");
                $fn = $this->input->post("fn");
                $mn = $this->input->post("mn");
                $ln = $this->input->post("ln");


                $newdata = array('suffix'=>$suffix,
                                'fn'=>$fn,
                                'mn'=>$mn,
                                'ln'=>$ln,

                                 );
                //this code is for the file upload
                           $config['upload_path'] = 'uploads';
                           $config['allowed_types'] = '*';

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

                           $data = array('upload_data' => $this->upload->data());
                 //end of file upload codes                 

                $this->db->insert('myself', $newdata); 

                redirect(base_url() . "site/complaint");    

            

        

查看(cmain.php)

<form action="<?php echo base_url();?>site/m1" name="details" id="details" method="post" enctype="multipart/form-data">
        <table class='table' >
            <tr>
            <td colspan='2'>
                <b><font color="#3B608C">Personal Information</font></b>
            </td>
        </tr>
        <tr>
            <td>
                Suffix  (e.g. Jr., III)
            </td>
            <td>
                <input type="text" name="suffix" id="suffix" value="">
            </td>
        </tr>
        <tr>
            <td>
                First Name*
            </td>
            <td>
                <input type="text" name="fn" id="fn" value="">
            </td>
        </tr>
        <tr>
            <td>
                Middle Name*
            </td>
            <td>
                <input type="text" name="mn" id="mn" value="">
            </td>
        </tr>
        <tr>
            <td>
                Last Name*
            </td>
            <td>
                <input type="text" name="ln" id="ln" value="">
            </td>
        </tr>
</table>

            <table>
        <tr>
        <td >
        Please attach documents pertinent to these complaints. <br>
        (Attach a zip file if more than one document)<br>
        </td>
        <td align="center">
        <input name = "userfile" type="file" class="input-xlarge" id = "userfile" />
        </td></tr>
        </table>
        <input type="submit" value="Submit Form" class="pull-right" id="submit" name="m1"/>
</form>

表格在数据库中正确发布,如后缀、fn、ln 和 mn 但是文件上传不起作用

我尽力遵循 codeigniter 文档的示例,只得到了我认为我需要的行

我做错了吗?

谢谢

【问题讨论】:

尝试使用验证类。然后将图片上传设置为自定义回调。 【参考方案1】:

终于搞定了

我用这个来上传文件

                //start of file upload code
                $config['upload_path'] = './uploads/';
                $config['allowed_types'] = '*';
                $this->load->library('upload', $config);
                $this->upload->do_upload();
                $data = $this->upload->data();
                //end of file upload code

并改变了

<form action="<?php echo base_url();?>site/m1" name="details" id="details" method="post" enctype="multipart/form-data">

进入

<?php echo form_open_multipart('site/c_upload');?>

【讨论】:

什么是c_upload?我在控制器中没有看到名为 c_upload 的函数【参考方案2】:

您是否为“上传”文件夹设置了写入权限? (例如:php.net/manual/en/function.chmod.php)

始终进行测试驱动开发。我从this url得到下面的代码块

if ( ! $this->upload->do_upload())

    $error = array('error' => $this->upload->display_errors());

    $this->load->view('upload_form', $error);

【讨论】:

【参考方案3】:

我认为您必须设置文件上传首选项

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';

表单助手

$this->load->helper(array('form', 'url'));

For further detail have a look at my blog post

【讨论】:

以上是关于Codeigniter 表单通过文件上传提交的主要内容,如果未能解决你的问题,请参考以下文章

将表单文件附加到电子邮件,Codeigniter

CodeIgniter - 表单验证和文件上传数据

如何在 Python 中模拟 post 表单来上传文件

Codeigniter 图片上传使用 jquery 和 ajax 错误 [重复]

通过ajax提交表单上传文件

如何在 Codeigniter 中上传文件名时添加前缀?