【问题标题】:Uploading image using Codeigniter (API)使用 Codeigniter (API) 上传图片
【发布时间】:2013-12-31 02:24:43
【问题描述】:

我正在尝试使用 codeigniter 在我的 API 中实现一个简单的图像上传功能。到目前为止,这是我的代码:

function addpicture_post()
{
      $config['upload_path'] = './upload/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '10000';
$config['max_width']  = '1024';
$config['max_height']  = '768';
$this->load->library('upload', $config);

if ( ! $this->upload->do_upload() )
    {
    print_r($this->upload->display_errors('', ''));
    print_r($this->upload->data());
    $info = 'not working';
    $this->response($info);
    }
}
}

当我使用apigee (https://apigee.com/console/others) 尝试时,如果我没有设置任何参数,我有以下消息:

HTTP/1.1 200 正常 状态: 200 日期: 格林威治标准时间 2013 年 12 月 12 日星期四 15:30:16 内容长度: 367 活着: 超时=15,最大值=100 内容类型: 应用程序/json X-Powered-By: PHP/5.3.5-1ubuntu7.11 服务器: Apache/2.2.17 (Ubuntu)

您没有选择要上传的文件。

到目前为止一切顺利。但是,当我尝试上传图像文件时(在远地点使用文件附件参数,我将其命名为“userfile”),我得到以下响应:

测试网址无效

我已经为此苦苦挣扎了几个小时,我试图查看代码点火器的文档,但不幸的是它与 API 无关,而且我无法弄清楚如何在不使用形式。

我不太确定问题是来自我用来测试它的服务、apigee,还是来自代码。最后,我的目标是在我正在编码的 iphone 应用程序中实现此功能,但我希望能够在尝试将其包含在我的应用程序中之前测试该功能。

任何人都可以给我有关 API 中文件上传的信息吗?以及如何正确测试它?谢谢。

【问题讨论】:

  • 提供view的代码
  • 除非你在做一些时髦的事情,否则我很确定你的上传路径不需要有 所以它应该是 /upload i> 不是 ./upload
  • 我已将 ./upload/ 更改为 /upload/,但仍然无法正常工作。我实际上没有任何看法,因为我只是在使用 Apigee 进行尝试,然后在 iOS 应用中实现它。

标签: php api codeigniter


【解决方案1】:

试试这个,它有效!

*输入文件名 = '图片'

function upload_image(){
 $img = array(); // return variable
 $this->load->helper(array('file','directory'));
 if (!empty($collection)) {
    $path="./uploads/";
    if( !is_dir($path) ) {
        mkdir($path);
    }
    $config['upload_path'] = $path; /* NB! create this dir! */
    $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
    $config['file_name'] = 'image001';
    $config['overwrite']=TRUE;

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


    $configThumb = array();
    $configThumb['image_library'] = 'gd2';
    $configThumb['source_image'] = '';
    $configThumb['create_thumb'] = FALSE;
    $configThumb['maintain_ratio'] = FALSE;

      /* Load the image library */
    $this->load->library('image_lib');

      /* We have 5 files to upload
       * If you want more - change the 6 below as needed
       */
        /* Handle the file upload */
    if (isset($_FILES['image']['tmp_name'])) {
        $upload = $this->upload->do_upload('image');

        /* File failed to upload - continue */
        if($upload === FALSE){
            $error = array('error' => $this->upload->display_errors());
            $data['message']=$error['error'];
            continue;   
        } 
        /* Get the data about the file */
        $data = $this->upload->data();
        $img['image']='/'.$data['file_name'];

    }

 }
        return $img;
}

【讨论】:

    【解决方案2】:

    试试这个,它可能对你有帮助,我是使用 codeigniter 框架工作的,这里是控制器

    function do_upload()
    {
    $config['upload_path']          = './uploads/';
    $config['allowed_types']        = 'gif|jpg|png';
    $config['max_size']             = 8000;
    $config['max_width']            = 1024;
    $config['max_height']           = 768;
    
    $this->load->library('upload', $config);
    if ( ! $this->upload->do_upload('userfile'))
    {
    $error = array('error' => $this->upload->display_errors());
    $this->load->view('upload_form', $error);
    }
    else
    {
    $data = array('upload_data' => $this->upload->data());
    $this->load->view('upload_success', $data);
    }
    }
    

    这是一个视图文件

    <?php echo $error;?>
    <?php echo form_open_multipart('upload/do_upload');?>
    <input type="file" name="userfile" size="20" />
    <input type="submit" value="upload" />
    

    【讨论】:

      【解决方案3】:

      在codeigniter中很简单,你也可以使用这个方法,从post中获取数据 method=multiform/form-data

      并使用此功能通过使用md5更改文件名来上传文件并将其存储在db中

      $md5 = md5(date('d-m-Y H:i:s'));    //md5 the constant here i have choosen date()
        if(!empty($_FILES['profile_pik']['name']))  //check whether file is there or not
        {
         
        $moveprofilepik='assets/uploads/profile_pik/' . $md5.str_replace(' ', '', $_FILES['d_profile_pik']['name']);   //append the location where you want to move and also concat the name using md5 value
         move_uploaded_file($_FILES['d_profile_pik']['tmp_name'], $move1);  //finally use inbuilt function move_uploaded_file and pass values 
       }
      

      【讨论】:

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