用php上传文件

时间:2014-11-02 08:13:12

标签: php html forms codeigniter file-upload

我在Code Igniter应用程序中进行了以下设置,但是文件没有传递到文件上传器类。这是页面上的第二个表单,仅供参考,我已检查它们是否都正确关闭。

当我选择有效图像时,我得到的输出是You did not select a file to upload.

查看

<form action="../albums/upload" class="form-horizontal" method="post">
    <div class="form-group">
        <input type="file" name="userfile" required="" class="form-control">
    </div>
    <div class="form-group">
        <div class="col-md-4 col-md-offset-4">
            <input type="submit" id="upload" name="upload" class="btn btn-success" value="Upload">
        </div>
    </div>
</form>

控制器

public function upload() {
    if($this->input->post('userfile')){
        $this->model_photo->do_upload();
    }
}

模型

function do_upload() {
    $path = base_url().'res/image/';
    $config = array(
        'allowed_types' => 'jpg|jpeg|gif|png|tif|tiff',
        'upload_path' => $path
    );

    $this->load->library('upload', $config);
    if ( ! $this->upload->do_upload('userfile')) {
        $error = array('error' => $this->upload->display_errors());
        echo $error['error']; //echo debugging purposes
    } else {
        $data = array('upload_data' => $this->upload->data());
        echo $data['upload_data']; //echo debugging purposes
    }
}

1 个答案:

答案 0 :(得分:1)

<form>添加enctype="multipart/form-data"

<form action="../albums/upload" class="form-horizontal" method="post" enctype="multipart/form-data">