无法使用codeigniter上传文件

时间:2014-04-17 18:01:14

标签: php codeigniter

上传文件的视图部分,即输入

<div class="step-pane" id="step4">
                            <div class="form-group m-t-lg"><label class="col-sm-3 control-label"> Pan Card </label>
                                <div class="col-sm-4">
                                    <input type="file" name="pan_card" title="Upload" class="btn btn-lg btn-info m-b-sm col-sm-12" data-required="true" data-trigger="change">
                                </div>
                            </div>
                            <div class="form-group m-t-lg"><label class="col-sm-3 control-label"> Last 3 months Bank Statements </label>
                                <div class="col-sm-4">
                                    <input type="file" name="statement1" title="Upload Statement 1" class="btn btn-lg btn-info m-b-sm col-sm-12" data-required="true" data-trigger="change">
                                    <input type="file" name="statement2" title="Upload Statement 2" class="btn btn-lg btn-info m-b-sm col-sm-12">
                                    <input type="file" name="statement3" title="Upload Statement 3" class="btn btn-lg btn-info m-b-sm col-sm-12">
                                </div>
                            </div>

我在控制器中有这个

if($data['success']==true)
                    {
                        $upload_path = FCPATH."merchant_activation_files/".$data['user_id'];
                        mkdir($upload_path);
                        $config =  array(
                          'allowed_types'   => "jpg|jpeg|gif|png|pdf|doc|docx|bmp|zip|rar|ods|odt|odp|rtf|txt",
                          'upload_path'     => $upload_path."/"
                        );
                        $this->upload->initialize($config);
                        $this->upload->do_upload('pan_card');
                        $this->upload->do_upload('statement1');
                        $this->upload->do_upload('statement2');
                        $this->upload->do_upload('statement3');

目录正在制作中,但文件没有上传。我已经为文件上传添加了codeiggniter库

2 个答案:

答案 0 :(得分:0)

您可以使用display_errors()方法向您显示无法正常工作的内容:

if(!$this->upload->do_upload('pan_card'))
{
    echo $this->upload->display_errors();
    exit;
}

但是,我通常会遇到上传课程的问题。我安装CodeIgniter时allowed_types似乎总是出错。如果我指定了允许的文件类型列表,则总是会出现错误,说明上传的文件类型是不允许的。

我建议将其更改为:

$config = array(
    'allowed_types' => '*',
    'upload_path' => $upload_path . '/'
);

检查是否有效。

答案 1 :(得分:0)

我拿了你的代码并用codeIgniter 2.1.0创建了一个小项目并重写了一些东西。它在我身边起作用,它不是你可以随意改变的最漂亮的解决方案。

我的观点:

<?php echo form_open_multipart(site_url().'/upload_test/do_upload');?>
    <div class="col-sm-4">
        <input type="file" name="pan_card" title="Upload" class="btn btn-lg                        btn-info m-b-sm col-sm-12" data-required="true" data-trigger="change">
        <input type="file" name="statement1" title="Upload Statement 1" class="btn btn-lg btn-info m-b-sm col-sm-12" data-required="true" data-trigger="change">
        <input type="file" name="statement2" title="Upload Statement 2" class="btn btn-lg btn-info m-b-sm col-sm-12">
        <input type="file" name="statement3" title="Upload Statement 3" class="btn btn-lg btn-info m-b-sm col-sm-12">
    </div>
    <input type="submit" value="Go Upload son !">

    <?php if(isset($all_files)): ?>
        <ul>
        <?php foreach ($all_files as $file): ?>
            <li><?php echo $file['file_name'] ;?></li>          
        <?php endforeach;?>
        </ul>
    <?php endif; ?>
</form>

我的控制员:

class Upload_test extends CI_Controller {

    public function do_upload()
    {

        $upload_path = "./merchant_activation_files/123/";

        if(!file_exists($upload_path)) {
             mkdir($upload_path);
        }

        $config =  array(
          'allowed_types'   => "jpg|jpeg|gif|png|pdf|doc|docx|bmp|zip|rar|ods|odt|odp|rtf|txt",
          'upload_path'     => $upload_path."/"
        );
        $this->load->library('upload', $config);

        # START UGLY PART DID PUT TOO MUCH TIME IN IT I JUST WANTED TO GIVE YOU THE IDEA

        $this->upload->do_upload('pan_card');
        $pan_card = $this->upload->data();

        $this->upload->do_upload('statement1');
        $statement1 = $this->upload->data();

        $this->upload->do_upload('statement2');
        $statement2 = $this->upload->data();

        $this->upload->do_upload('statement3');
        $statement3 = $this->upload->data();

        # END UGLY PART

        $data['all_files'] = array($pan_card, $statement1, $statement2, $statement3);

        $this->load->view('welcome_message', $data);
    }
}