使用phonegap和php上传base64图像和音频文件

时间:2013-05-23 05:48:11

标签: php jquery cordova

这是用于上传捕获的音频的phonogap代码...

 function uploadFile(mediaFile) {
                var ft = new FileTransfer(),
                    path = mediaFile.fullPath,
                    name = mediaFile.name;  //audio comes here...path and name of file
         var img64 = imgdata; // here comes image in base64 and will decode at php in server side

ft.upload(path,
        "http://my.domain.com/upload.php",
        function(result) {
            console.log('Upload success: ' + result.responseCode);
            console.log(result.bytesSent + ' bytes sent');
        },
        function(error) {
            console.log('Error uploading file ' + path + ': ' + error.code);
        },
        { fileName: name });   
}

我想使用该Fileuploader上传基本64和音频文件中的图像数据,并将PHP存储到网址

在PHP中

    $img = $_POST['image'];
    $img = str_replace(' ', '+', $img);
    $data = base64_decode($img); // FOR AUDIO how do i GET ?

1 个答案:

答案 0 :(得分:1)

为什么不使用$_FILES代替base64编码的$_POST

PHP手册

发布方法上传http://www.php.net/manual/en/features.file-upload.post-method.php

PhoneGap参考

FileTransfer http://docs.phonegap.com/en/2.7.0/cordova_file_file.md.html#FileTransfer FileTransferOptions http://docs.phonegap.com/en/2.7.0/cordova_file_file.md.html#FileUploadOptions

这些陈述似乎非常重要:

<强> fileKey
The name of the form element. If not set defaults to file . (DOMString)

<强> fileName
The file name you want the file to be saved as on the server. If not set defaults to image.jpg . (DOMString)

示例:

<?php

$upload_key = 'file';

if (isset($_FILES[$upload_key])) {

    try {

        $error = $_FILES[$upload_key]['error'];
        if (is_array($error))
            throw new Exception('This script can\'t accept multiple files');
        switch ($error) {
            case UPLOAD_ERR_INI_SIZE:
                throw new Exception('Exceeded upload_max_filesize');
            case UPLOAD_ERR_FORM_SIZE:
                throw new Exception('Exceeded MAX_FILE_SIZE');
            case UPLOAD_ERR_PARTIAL:
                throw new Exception('Incomplete file uploaded');
            case UPLOAD_ERR_NO_FILE:
                throw new Exception('No file uploaded');
            case UPLOAD_ERR_NO_TMP_DIR:
                throw new Exception('No tmp directory');
            case UPLOAD_ERR_CANT_WRITE:
                throw new Exception('Can\'t write data');
            case UPLOAD_ERR_EXTENSION:
                throw new Exception('Extension error');
        }

        $finfo    = new finfo(FILEINFO_MIME);
        $name     = $_FILES[$upload_key]['name'];
        $tmp_name = $_FILES[$upload_key]['tmp_name'];
        $size     = $_FILES[$upload_key]['size'];

        if ($size > 1000000)
            throw new Exception('Exceeded 1MB limit');
        if (!is_uploaded_file($tmp_name))
            throw new Exception('Not an uploaded file');

        $type = $finfo->file($tmp_name);

        if ($type === false)
            throw new Exception('Failed to get MimeType');
        if (substr($type, 'image/') !== 0);
            throw new Exception('Only images available');

        $new_name = dirname(__FILE__).'/upload/'.$name;

        if (is_file($new_name))
            throw new Exception("The file {$new_name} already exists");

        if (!move_uploaded_file($tmp_name, $new_name))
            throw new Exception('Failed to move uploaded file');

        $msg = "File successfully uploaded as {$new_name}";

    } catch (Exception $e) {

        $msg = 'Error: '.$e->getMessage();

    }

} else {

    $msg = 'No file sent';

}

echo $msg;