1

这是用于上传捕获的音频的 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 上传 base 64 中的图像数据和音频文件,并在 PHP 中存储到 url

在 PHP 中

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

1 回答 1

1

你为什么不使用$_FILES而不是 base64-encoded $_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 FileTransferOptionshttp : //docs.phonegap.com/en/2.7.0/cordova_file_file.md.html#FileUploadOptions

这些陈述似乎非常重要:

fileKey
The name of the form element. If not set defaults tofile. (DOMString)

fileName
The file name you want the file to be saved as on the server. If not set defaults toimage.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;
于 2013-05-23T06:43:33.063 回答