Codeigniter和RestServer。如何上传图片?

时间:2011-08-16 11:42:26

标签: codeigniter rest upload

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

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

感谢所有输入!

5 个答案:

答案 0 :(得分:11)

确定,

此处还有另一种解决方案:FIle upload from a rest client to a rest server

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

然而,这是有效的;实际上工作了。

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

function enter_post()
    {

        $this->response($_FILES);
    }

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

您还可以输出:

  

$这 - >响应($ _ SERVER);

  

$这 - >响应($ _ POST);

我得到了以下JSON输出:

  

{ “文件”:{ “名称”: “camel.jpg”, “类型”: “应用/八位字节流”, “tmp_name的值”: “/ TMP / phpVy8ple”, “错误”:0,“尺寸“: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';        
             }

答案 1 :(得分:5)

您实际上可以使用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);
    }
}

答案 2 :(得分:2)

尝试此代码,图像以时间戳及其扩展名保存..

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

答案 3 :(得分:0)

在观察亚马逊s3如何做到http://www.anyexample.com/programming/php/uploading_files_to_amazon_s3_with_rest_api.xml

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

然后在服务器上我假设它会使用file_put_contents()和请求的主体来保存文件。

答案 4 :(得分:0)

以下是在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));           return $ filenames;          }

这是定义将图像副本作为小缩略图上载到不同文件夹的功能 function fullimage_upload1($ data)     {         $这 - >负载>库(' image_lib&#39);

    //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'];
}