0

我正在尝试使用服务器端的 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);
    }
}

} ?>

这是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);

这是日志猫

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,它可以工作。

更新:

对于其他尝试从 phonegap 上传上传的人,请使用此代码

对于来自http://zacvineyard.com/blog/2011/03/upload-a-file-to-a-remote-server-with-phonegap的普通 php

print_r($_FILES); //to check details of file
$new_image_name = "namethisimage.jpg";
move_uploaded_file($_FILES["file"]["tmp_name"], "./uploads/".$new_image_name);

对于代码点火器

if ( ! $this->upload->do_upload('file'))// file should be stated as dakdad answered

谢谢

4

1 回答 1

0

do_upload调用需要正确的文件字段名称参数。默认值userfile在您的示例中是file. 所以该行应该是:

if ( ! $this->upload->do_upload('file'))
于 2013-03-06T03:25:33.463 回答