Codeigniter REST上传多个图像

时间:2012-12-19 05:15:25

标签: php codeigniter rest codeigniter-2

我绝对坚持这个...检查所有相关的stackoverflow帖子,没有任何帮助。在Codeigniter中使用Phil的REST服务器/客户端设置,可以插入普通条目,但不能上传多个图像,实际上甚至是单个图像。

我无法确定如何调试REST部分,它只返回任何内容。

我从视图中输入了这个数组:

Array
(
    [1] => Array
        (
            [name] => sideshows.jpg
            [type] => image/jpeg
            [tmp_name] => /Applications/MAMP/tmp/php/phppYycdA
            [error] => 0
            [size] => 967656
        )

    [2] => Array
        (
            [name] => the-beer-scale.jpg
            [type] => image/jpeg
            [tmp_name] => /Applications/MAMP/tmp/php/phpCsiunQ
            [error] => 0
            [size] => 742219
        )

    [3] => Array
        (
            [name] => the-little-lace.jpg
            [type] => image/jpeg
            [tmp_name] => /Applications/MAMP/tmp/php/phpXjT7WL
            [error] => 0
            [size] => 939963
        )

    [4] => Array
        (
            [name] => varrstoen-australia.jpg
            [type] => image/jpeg
            [tmp_name] => /Applications/MAMP/tmp/php/phpcHrJXe
            [error] => 0
            [size] => 2204400
        )

)

我正在使用我发现的帮助方法来整理多个文件上传:

function multifile_array()
{
    if(count($_FILES) == 0)
        return;

    $files = array();
    $all_files = $_FILES['files']['name'];
    $i = 0;

    foreach ($all_files as $filename) {
        $files[++$i]['name'] = $filename;
        $files[$i]['type'] = current($_FILES['files']['type']);
        next($_FILES['files']['type']);
        $files[$i]['tmp_name'] = current($_FILES['files']['tmp_name']);
        next($_FILES['files']['tmp_name']);
        $files[$i]['error'] = current($_FILES['files']['error']);
        next($_FILES['files']['error']);
        $files[$i]['size'] = current($_FILES['files']['size']);
        next($_FILES['files']['size']);
    }

    $_FILES = $files;

}

在API控制器中调用此函数:

public function my_file_upload_post() {

        if( ! $this->post('submit')) {
            $this->response(NULL, 400);
        }

        $data = $this->post('data');
        multifile_array();

        $foldername = './uploads/' . $this->post('property_id');

        if(!file_exists($foldername) && !is_dir($foldername)) {
            mkdir($foldername, 0750, true);
        }

        $config['upload_path'] = $foldername;
        $config['allowed_types'] = 'gif|jpg|png|doc|docx|pdf|xlsx|xls|txt';
        $config['max_size'] = '10240';

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

        foreach ($data as $file => $file_data) {
            $this->upload->do_upload($file);

            //echo '<pre>';
            //print_r($this->upload->data());
            //echo '</pre>';

      }


        if ( ! $this->upload->do_upload() ) {
            return $this->response(array('error' => strip_tags($this->upload->display_errors())), 404);
        } else {
            $upload = $this->upload->data();
            return $this->response($upload, 200);

        }



    }

我很高兴分享我的代码,我确实设法上传多个文件而不用担心,但只是尝试使用API​​进行设置,以便我可以从外部网站,内部或iPhone上调用它。帮助将不胜感激。

干杯

更新

以下是API控制器的代码:

function upload_post() {

        $foldername = './uploads/' . $this->post('property_id');

        if(!file_exists($foldername) && !is_dir($foldername)) {
            mkdir($foldername, 0750, true);
        }

        $config['upload_path'] = $foldername;
        $config['allowed_types'] = 'gif|jpg|png|doc|docx|pdf|xlsx|xls|txt';
        $config['max_size'] = '10240';

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

        if ( ! $this->upload->do_upload())
        {
            //return $this->response(array('error' => strip_tags($this->upload->display_errors())), 404);
            return $this->response(array('error' => var_export($this->post('file'), true)), 404);

        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
            return $this->response(array('error' => $data['upload_data']), 404);
        }
return $this->response(array('success' => 'successfully uploaded' ), 200);      
}

如果您从表单直接访问API,则可以使用此功能,但您需要在浏览器中输入用户名和密码。如果我通过控制器运行它,那么它无法找到图像。

1 个答案:

答案 0 :(得分:1)

这不是你要求的,但这就是我用来解决这个问题的方法。一个PHP / jQuery上传小部件!

http://blueimp.github.com/jQuery-File-Upload/