REST CODEIGNITER多个上传文件

时间:2015-02-12 14:41:16

标签: php codeigniter rest

我尝试使用https://github.com/chriskacerguis/codeigniter-restserver

构建REST API使用codeigniter插件

我用这个成功构建了多个上传,但是在上传许多文件数据时我遇到了麻烦。 当我在我的目录中选择3个文件时,我的代码可以工作,但在上传路径中我只有2个文件。

这里控制器:

function upload_post()
{
   $name_array = array();
    $count = count($_FILES['userfile']['size']);
    foreach($_FILES as $key=>$value)
    {
    for($s=0; $s<=$count-1; ) {
        $_FILES['userfile']['name']=$value['name'][$s];
        $_FILES['userfile']['type']    = $value['type'][$s];
        $_FILES['userfile']['tmp_name'] = $value['tmp_name'][$s];
        $_FILES['userfile']['error']       = $value['error'][$s];
        $_FILES['userfile']['size']    = $value['size'][$s];  
            $config['upload_path'] = 'E:/tes/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100000';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        $this->load->library('upload', $config);
        $this->upload->do_upload();
        $data = $this->upload->data();
        $name_array[] = $data['file_name'];
        $s++;
    }
}
    $names= implode(',', $name_array);
    /* $this->load->database();
    $db_data = array('id'=> NULL,
    'name'=> $names);
    $this->db->insert('testtable',$db_data);
    */ print_r($names);
    print_r($count);
}

1 个答案:

答案 0 :(得分:1)

我为我做了一个帮助函数,它上传文件并返回一个带有上传结果的数组。它需要一个参数上传目录来保存文件。这是我的功能

function upload_file($save_dir="images")//takes folder name where to save.default images folder
{
    $CI = & get_instance();
    $CI->load->library('upload');
    $config=array();
    $config['upload_path'] = FCPATH.$save_dir;
    $config['allowed_types'] = 'gif|jpg|png';//only images.you can set more
    $config['max_size'] = 1024*10;//10 mb you can increase more.Make sure your php.ini has congifured same or more value like this
    $config['overwrite'] = false;//if file do not replace.create new file
    $config['remove_spaces'] = true;//remove sapces from file
    //you can set more config like height width for images

    $uploaded_files=array();
    foreach ($_FILES as $key => $value)
    {
        if(strlen($value['name'])>0)
        {
            $CI->upload->initialize($config);
            if (!$CI->upload->do_upload($key))
            {
                $uploaded_files[$key]=array("status"=>false,"message"=>$value['name'].': '.$CI->upload->display_errors());
            }
            else
            {
                $uploaded_files[$key]=array("status"=>true,"info"=>$CI->upload->data());
            }

        }

    }
    return $uploaded_files;

}

示例输出
如果你打印出函数返回,你会得到像这样的输出

Array
(
    [file_input_name1] => Array
        (
            [status] => 1//status will be true if uploaded
            [info] => Array //if success it will return the file informattion
            (
                    [file_name] => newfilename.jpg
                    [file_type] => image/jpeg
                    [file_path] => C:/Program Files (x86)/VertrigoServ/www/rnd/images/
                    [full_path] => C:/Program Files (x86)/VertrigoServ/www/rnd/images/newfilename.jpg
                    [raw_name] => newfilename
                    [orig_name] => newfilename.jpg
                    [client_name] => newfilename
                    [file_ext] => .jpg
                    [file_size] => 762.53
                    [is_image] => 1
                    [image_width] => 1024
                    [image_height] => 768
                    [image_type] => jpeg
                    [image_size_str] => width="1024" height="768"
                )

        )


    [file_input_name_2] => Array
    (
         [status] =>//if invalid file status will be false with error message
        [message] => desktop.ini: <p>The filetype you are attempting to upload is not allowed.</p> 
        )

)

希望它可以帮到你