【问题标题】:Codeigniter and RestServer. How to upload images?Codeigniter 和 RestServer。如何上传图片?
【发布时间】:2011-10-28 00:08:24
【问题描述】:

我正在使用 Codeigniter 中的 Phils RestServer(请参阅链接)编写 API。 我需要一种通过 API 上传图片的方法。我怎样才能做到这一点? 是否就像使用正确的标头(使用什么标头)发出 POST 请求一样简单?

https://github.com/philsturgeon/codeigniter-restserver

感谢所有输入!

【问题讨论】:

    标签: codeigniter rest upload


    【解决方案1】:

    好的,

    这里还有一个解决方案:FIle upload from a rest client to a rest server

    但这些解决方案都不适合我。

    但是,这是行得通的;确实有效。

    首先,我不确定我的文件是否到达方法,所以我将响应行更改为:

    function enter_post()
        {
    
            $this->response($_FILES);
        }
    

    注意,这是测试 REST 方法的好方法。

    也可以输出:

    $this->响应($_SERVER);

    $this->response($_POST);

    等等

    我得到以下 JSON 输出:

    {"file":{"name":"camel.jpg","type":"application/octet-stream","tmp_name":"/tmp/phpVy8ple","error":0,"size ":102838}}

    所以我知道我的文件在那里。

    然后我更改了查找和移动文件的方法。我使用通用文件访问脚本从临时位置获取文件并将其移动到新位置:

        $uploaddir = '/home/me/public_html/uploads/';
    
        $uploadfile = $uploaddir . basename($_FILES['file']['name']);
    
        if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
            $data['status'] = 'uploaded';       
                } else {
            $data['status'] =  'failed';        
                 }
    

    【讨论】:

      【解决方案2】:

      您实际上可以使用 CodeIgniter 的原生 File Upload Class 来方便使用 Phil's Rest-Server 进行上传,如下所示:

      /**
       * REST API Upload using native CI Upload class.
       * @param userfile - multipart/form-data file
       * @param submit - must be non-null value.
       * @return upload data array || error string
       */
      function upload_post(){
          if( ! $this->post('submit')) {
              $this->response(NULL, 400);
          }
          $this->load->library('upload');
      
          if ( ! $this->upload->do_upload() ) {
              $this->response(array('error' => strip_tags($this->upload->display_errors())), 404);
          } else {
              $upload = $this->upload->data();
              $this->response($upload, 200);
          }
      }
      

      【讨论】:

        【解决方案3】:

        试试这个代码,图像保存时带有时间戳及其扩展名..

        $uploaddir = 'uploads/';
            $path = $_FILES['image_param']['name'];
            $ext = pathinfo($path, PATHINFO_EXTENSION);
            $user_img = time() . rand() . '.' . $ext;
            $uploadfile = $uploaddir . $user_img;
            if ($_FILES["image_param"]["name"]) {
                if (move_uploaded_file($_FILES["image_param"]["tmp_name"],$uploadfile)) {
        echo "success";
            }
        

        【讨论】:

          【解决方案4】:

          在这里查看 amazon s3 是如何做到的http://www.anyexample.com/programming/php/uploading_files_to_amazon_s3_with_rest_api.xml

          看起来您在客户端定义了文件类型,然后将其加载到带有file_get_contents($file_path) 的变量中,然后使用示例中定义的适当标头发送它。

          然后在服务器上,我假设它将使用 file_put_contents() 和请求正文来保存文件。

          【讨论】:

            【解决方案5】:

            以下是form()函数中控制器Form.php中图片路径及其属性设置的配置

                $config['upload_path']              = 'uploads/images/full';
                $config['allowed_types']            = 'gif|jpg|png';
                $config['max_width']                = '6000';
                $config['max_height']               = '6000';
                $config['encrypt_name']             = true;
                $this->load->library('upload', $config);
                $this->upload->initialize($config); 
                $uploaded               = $this->upload->do_upload($imgfile);
                if($uploaded)
            

            { $filenames = $this->fullimage_upload1($this->upload->data($imgfile)); 返回$文件名; }

            这是定义将图像副本上传到不同文件夹的功能,缩略图 函数 fullimage_upload1($data) { $this->load->library('image_lib');

                //this is the larger image
                $config['image_library'] = 'gd2';
                $config['source_image'] = 'uploads/images/full/'.$data['file_name'];
                $config['new_image']    = 'uploads/images/small/'.$data['file_name'];
                $config['maintain_ratio'] = TRUE;
                /*$config['width'] = 600;
                $config['height'] = 500;*/
                $this->image_lib->initialize($config);
                $this->image_lib->resize();
                $this->image_lib->clear();
            
                //cropped thumbnail
                $config['image_library'] = 'gd2';
                $config['source_image'] = 'uploads/images/full/'.$data['file_name'];
                $config['new_image']    = 'uploads/images/thumbnails/'.$data['file_name'];
                $config['maintain_ratio'] = TRUE;
                $config['width'] = 300;
                $config['height'] = 258;
                $this->image_lib->initialize($config);  
                $this->image_lib->resize(); 
                $this->image_lib->clear();
            
                return $data['file_name'];
            }
            

            【讨论】:

              猜你喜欢
              • 2013-08-23
              • 2011-06-08
              • 2012-01-22
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-11-30
              相关资源
              最近更新 更多