Codeigniter and RestServer. How to upload images?

2019-03-27 09:10发布

I am coding an API using Phils RestServer (see link) in Codeigniter. I need a way to upload images via the API. How can I do that? Is it just as simple as making a POST request with the correct headers (what headers to use)?

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

Thankful for all input!

5条回答
乱世女痞
2楼-- · 2019-03-27 09:47

Ok,

There is another solution here: FIle upload from a rest client to a rest server

But neither of these solutions worked for me.

However, this is what worked; actually worked.

First, I wasn't sure if my file was reaching the method, so I changed the response line to:

function enter_post()
    {

        $this->response($_FILES);
    }

Note, this is a great way to test your REST methods.

You can also output:

$this->response($_SERVER);

and

$this->response($_POST);

etc.

I got the following JSON output:

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

So I knew my file was there.

I then changed the method to find and move the file. I used common file access script to get the file from its temp location and move it to a new location:

    $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';        
             }
查看更多
3楼-- · 2019-03-27 09:47

In looking at how amazon s3 does it here http://www.anyexample.com/programming/php/uploading_files_to_amazon_s3_with_rest_api.xml

It looks like on the client side you define the file type and then load it up in a variable with file_get_contents($file_path) and then send it with the appropriate headers defined in the example.

Then on the server I assume it would be using file_put_contents() with the body of the request to save the file.

查看更多
来,给爷笑一个
4楼-- · 2019-03-27 09:48

try this code ,image is saved with time stamp and its extension ..

$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";
    }
查看更多
劳资没心,怎么记你
5楼-- · 2019-03-27 09:49

You can actually use CodeIgniter's native File Upload Class to facilitate uploads with Phil's Rest-Server like this:

/**
 * 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);
    }
}
查看更多
干净又极端
6楼-- · 2019-03-27 09:50

Following is configuration of image path and their respective attributes setting in controller Form.php in form() function

    $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)); return $filenames; }

This is the function defining upload the image copy to the different folders as small,thumbnails function 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'];
}
查看更多
登录 后发表回答