我正在尝试使用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 OK状态:200日期:2013年12月12日星期四15:30:16 GMT内容长度:367保持活力:超时=15,最大值=100内容类型:application/jsonX电源:PHP/5.3.5-1ubuntu7.11服务器:Apache/2.2.17(Ubuntu)
您没有选择要上载的文件。
到目前为止还不错。但当我试图上传一个图像文件(文件附件参数在远地点,我将其命名为"userfile")时,我会得到以下响应:
无效的测试url
几个小时以来,我一直在努力解决这个问题,我试图查看代码点火器的文档,但不幸的是,它与API无关,而且我不知道如何在不使用表单的情况下上传文件。
我不太确定问题是来自我用来测试它的服务,还是来自代码。最后,我的目标是在我正在编码的iphone应用程序中实现这个功能,但我希望在尝试将它包含在我的应用程序中之前能够测试这个功能。
有人可以给我在API上传文件的信息吗?如何正确测试?非常感谢。
试试这个,它有效!
*输入文件名="图像"
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;
}
试试这个,它可能会对你有所帮助,我使用代码点火器框架工作完成了这项工作,这是控制器
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" />
它在codeigniter中很简单,你也可以使用这个方法,从post中获取数据方法=多种形式/表单数据
并使用此函数通过使用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
}