我正在尝试使用服务器端的phonegap和codeigniter将图片上传到服务器
这是php代码
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Upload extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '1600';
$config['max_height'] = '1600';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
echo $error;
//$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
echo "success";
//$this->load->view('upload_success', $data);
}
}
} ?&GT;
这是phonegap中的代码
var options = new FileUploadOptions();
options.fileKey="file";
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";
options.chunkedMode = false;
var params = {};
params.value1 = "test";
params.value2 = "param";
options.params = params;
var ft = new FileTransfer();
ft.upload(imageURI, encodeURI("http://mywebsite.com/upload/do_upload"), win, fail, options, true);
这是logcat
03-06 10:29:52.510: D/FileTransfer(5732): upload file:///mnt/sdcard/Android/data/com.fileupload.test/cache/1362536991781.jpg to http://soovy.me/upload/do_upload
03-06 10:29:52.510: D/FileTransfer(5732): fileKey: file
03-06 10:29:52.510: D/FileTransfer(5732): fileName: 1362536991781.jpg
03-06 10:29:52.510: D/FileTransfer(5732): mimeType: image/jpeg
03-06 10:29:52.510: D/FileTransfer(5732): params: {"value1":"test","value2":"param"}
03-06 10:29:52.510: D/FileTransfer(5732): trustEveryone: true
03-06 10:29:52.510: D/FileTransfer(5732): chunkedMode: false
03-06 10:29:52.510: D/FileTransfer(5732): headers: null
03-06 10:29:52.510: D/FileTransfer(5732): objectId: 1
03-06 10:29:52.540: D/FileTransfer(5732): String Length: 256
03-06 10:29:52.540: D/FileTransfer(5732): Content Length: 23677
03-06 10:29:54.930: D/FileTransfer(5732): got response from server
03-06 10:29:54.930: D/FileTransfer(5732): <p>You did not select a file to upload.</p>
03-06 10:29:54.940: D/CordovaLog(5732): Code = 200
03-06 10:29:54.940: I/Web Console(5732): Code = 200 at file:///android_asset/www/index.html:400
03-06 10:29:54.950: D/CordovaLog(5732): Response = <p>You did not select a file to upload.</p>
03-06 10:29:54.950: I/Web Console(5732): Response = <p>You did not select a file to upload.</p> at file:///android_asset/www/index.html:401
03-06 10:29:54.950: D/CordovaLog(5732): Sent = 23421
03-06 10:29:54.950: I/Web Console(5732): Sent = 23421 at file:///android_asset/www/index.html:402
然而我在php上遇到错误您没有选择要上传的文件但是因为logcat声明它发送23421所以发送了图像。我还用一个发送图像的普通表单测试了php,它运行起来。
更新:
对于尝试从手机上传上传的其他人,请使用此代码
来自http://zacvineyard.com/blog/2011/03/upload-a-file-to-a-remote-server-with-phonegap
的普通phpprint_r($_FILES); //to check details of file
$new_image_name = "namethisimage.jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "./uploads/".$new_image_name);
对于codeigniter
if ( ! $this->upload->do_upload('file'))// file should be stated as dakdad answered
由于
答案 0 :(得分:0)
do_upload
调用需要正确的文件字段名称参数。您的示例中的默认值为userfile
,为file
。所以该行应该是:
if ( ! $this->upload->do_upload('file'))