Codeigniter REST上传多张图片

4

我完全被卡住了...查看了所有相关的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个回答

1

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接