Codeigniter文件上传不起作用

时间:2014-06-25 13:35:06

标签: codeigniter file-upload codeigniter-form-helper

我在尝试在我的应用程序中实现Codeigniter类时遇到问题。问题是,当我提交表单时,执行的函数不是将文件保存到我指定的文件夹,而是不会抛出错误消息。我的代码如下:

查看:

<?php echo form_open_multipart('requests/submit_candidate'); ?>
<table>
        <tr>
        <td><label for="text">Upload CV</label></td>
        <td><input type="file" name="file"></textarea></td>
    </tr>
    <tr>
        <td><label for="text">Extra Information</label></td>
        <td><textarea name="extra_info"></textarea></td>
    </tr>       
</table>
<input type="submit" name="submit" value="Submit" />
</form>

控制器:

public function submit_candidate($slug)
    {
        $this->load->library('upload');

        $config =  array(
              'upload_path'     => dirname($_SERVER["SCRIPT_FILENAME"])."/uploads/",
              'upload_url'      => base_url()."uploads/",
              'allowed_types'   => "gif|jpg|png|jpeg|pdf|doc|xml",
              'overwrite'       => TRUE,
              'max_size'        => "1000KB",
              'max_height'      => "768",
              'max_width'       => "1024"  
            );

        if($this->upload->do_upload($config))
        {
            echo "file upload success";
        }
        else
        {
           echo "file upload failed";
        }
}

有人可以帮忙吗?

非常感谢,

SR

1 个答案:

答案 0 :(得分:0)

如果适合您,请尝试此操作:

1) HTML

<td><input type="file" name="file"></td>

2)控制器

public function submit_candidate($slug)
{
    $this->load->library('upload');

    $config =  array(
          'upload_path'     => "./uploads/",
          //'upload_url'      => base_url()."uploads/",
          'allowed_types'   => "gif|jpg|png|jpeg|pdf|doc|xml",
          'overwrite'       => TRUE,
          'max_size'        => 1000,
          'max_height'      => 768,
          'max_width'       => 1024  
        );
    $this->upload->initialize( $config );
    if(!$this->upload->do_upload('file'))  // "file" is the name of the element default it's "userfile"
    {
        print_r( $this->upload->display_errors() );die; //display the error
    }
    else
    {
       $data    = $this->upload->data();
       print_r( $data );die;                            //file uploaded data
    }
}

1)在函数内部,您需要提供表单文件元素的名称(对于它的“文件”)
2)您还使用了无效的配置变量“upload_url” 3)您需要初始化上传库的配置 这里(File Upload Documentation)是您的参考。