Codeigniter:通过手机上传文件

时间:2017-07-25 11:09:37

标签: php codeigniter

我正在使用以下代码上传文件。

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

            $files = $_FILES;

            $cpt = count($_FILES['userfile']['name']);
            $this->data['data']= $files; 

            $this->upload->initialize($this->set_upload_options());
            for($i=0; $i<$cpt; $i++) { 

                $_FILES['userfile']['name']= $files['userfile']['name'][$i];
                $_FILES['userfile']['type']= $files['userfile']['type'][$i];
                $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
                $_FILES['userfile']['error']= $files['userfile']['error'][$i];
                $_FILES['userfile']['size']= $files['userfile']['size'][$i];    


                $this->data['name']= $_FILES['userfile']['name']; 
                $this->data['type']= $_FILES['userfile']['type']; 

                if($this->upload->do_upload()){
                     $this->db->insert('attachment', array(
                        'type'      =>   'quotation',
                        'typeid'    =>   $lastid, 
                        'path'      =>   $_FILES['userfile']['name'],
                        'extension' =>   $_FILES['userfile']['type'],
                     ));
                    $this->data['message'] = 'File Uplaoded';

                }
                else {
                    $this->data['message'] = $this->upload->display_errors(); 

                }
            }
            echo json_encode($this->data); die;


private function set_upload_options()
    {   
        //upload an image options
        $config = array();
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png|mp4|jpeg';
        $config['max_size']      = '*';
        $config['overwrite']     = FALSE;


        return $config;
    }

以下是我们收到的输出

 data =     {
        userfile =         {
            error = 0;
            name = imgname;
            size = 146245;
            "tmp_name" = "/tmp/phppYlH8A";
            type = "image/jpeg";
        };
    };
    message = "<p>You did not select a file to upload.</p>";
    name = s;
    type = i;

这就是移动开发者的回应。

如果我尝试跟踪文件上传

 <form action="formlink" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<input type="file" name="userfile[]"   />
<input type="file" name="userfile[]"   />

<br /><br />

<input type="submit" value="upload" />

</form>

请帮助我如何解决这个问题

1 个答案:

答案 0 :(得分:0)

您需要使用以下代码上传数据:

$config = array(
            'upload_path' => $path,
            'allowed_types' => 'jpg|gif|png|jpeg',
            'overwrite' => 1,
            );
            $this->load->library('upload', $config);
            $images = array();
            foreach ($files['name'] as $key => $image) {
                if ($image != '') {
                    $_FILES['userfile[]']['name'] = $files['name'][$key];
                    $_FILES['userfile[]']['type'] = $files['type'][$key];
                    $_FILES['userfile[]']['tmp_name'] = $files['tmp_name'][$key];
                    $_FILES['userfile[]']['error'] = $files['error'][$key];
                    $_FILES['userfile[]']['size'] = $files['size'][$key];
                    $image = str_replace(' ', '_', $image);
                    $fileName = time() . '_' . $image;
                    $images[] = $fileName;
                    $config['file_name'] = $fileName;
                    $this->upload->initialize($config);
                    if ($this->upload->do_upload('images[]')) {
                        $this->upload->data();
                    } else {
                        return false;
                    }
                }
            }