【问题标题】:Codeigniter and RestServer. How to upload images?Codeigniter 和 RestServer。如何上传图片?
【发布时间】:2011-10-28 00:08:24
【问题描述】:
【问题讨论】:
标签:
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";
}
【解决方案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'];
}