加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – Codeigniter REST上传多个图像

发布时间:2020-12-13 22:46:48 所属栏目:PHP教程 来源:网络整理
导读:我完全坚持这个…检查所有相关的stackoverflow帖子,没有任何帮助.在Codeigniter中使用Phil的REST服务器/客户端设置,可以插入普通条目,但不能上传多个图像,实际上甚至是单个图像. 我无法确定如何调试REST部分,它只返回任何内容. 我有这个数组来自视图: Array
我完全坚持这个…检查所有相关的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,$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,则可以使用此选项,但您需要在浏览器中输入用户名和密码.如果我通过控制器运行它,那么它无法找到图像.

解决方法

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读