简体   繁体   中英

Uploading image using Codeigniter (API)

I'm trying to implement a simple image upload function in my API using codeigniter. So far this is my code :

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);
    }
}
}

When I'm using apigee ( https://apigee.com/console/others ) to try it, if I do not set any parameter, I have the following message :

HTTP/1.1 200 OK Status: 200 Date: Thu, 12 Dec 2013 15:30:16 GMT Content-Length: 367 Keep-Alive: timeout=15, max=100 Content-Type: application/json X-Powered-By: PHP/5.3.5-1ubuntu7.11 Server: Apache/2.2.17 (Ubuntu)

You did not select a file to upload.

So far so good. But when I'm trying to upload an image file (with the file attachement parameter in apogee, which I named "userfile"), I have the following response :

Invalid test url

I'm fairly struggling with that for a few hours now, I've tried to look at code igniter's documentation but unfortunately it has nothing to do with APIs , and I can't figure out how to upload files without using a form.

I'm not quite sure if the problem is coming from the service I'm using to test it, apigee, or if the problem is coming from the code. At the end, my goal is to implement this function in an iphone app I'm coding, but I'd like to be able to test the function before trying to include it in my app.

Anyone could give me informations about file upload within an API ? And how to test it properly ? Thank you.

Try This, it works!

*input file name = 'image'

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;
}

Try this, it might help you, I did this using codeigniter frame work, here is the controller

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);
}
}

here is a view file

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

Its simple in codeigniter you can use this method also , take data from post method=multiform/form-data

And use this function for uploading the file by changing its name using md5 and store it in 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 
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM