繁体   English   中英

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

[英]Upload a base64 img and audio file with phonegap and php

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

 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格式存储到url

在PHP中

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

为什么不使用$_FILES而不是base64编码的$_POST呢?

PHP手册

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

PhoneGap参考

文件传输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 The name of the form element. If not set defaults to file . (DOMString) . (DOMString)

fileName
The file name you want the file to be saved as on the server. If not set defaults to The file name you want the file to be saved as on the server. If not set defaults to image.jpg . (DOMString) . (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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM