上传到服务器时,PhoneGap文件传输会破坏.mp3

时间:2013-05-13 13:56:04

标签: javascript php cordova

我正在为一个项目搞乱PhoneGap,但我在上传.mp3时遇到了一些麻烦

我可以使用设备的内置麦克风录制mp3,然后我可以使用以下JavaScript上传文件:

var audio_name = 'fyp_app_recording_' + Math.round(new Date().getTime()/1000) + '.mp3';

// Record audio
// 
function recordAudio() {

    var src = audio_name;
    var mediaRec = new Media(src, onSuccess(src), onError);

    // Record audio
    mediaRec.startRecord();

    // Stop recording after 10 sec
    var recTime = 0;
    var recInterval = setInterval(function() {
        recTime = recTime + 1;
        setAudioPosition("Recording Audio - " + recTime + " sec");
        if (recTime >= 10) {
            clearInterval(recInterval);
            mediaRec.stopRecord();
            setAudioPosition("Recording Saved As " + src);
        }
    }, 1000);
}

// onSuccess Callback
//
function onSuccess(src) {
    console.log("recordAudio():Audio Success" + src);
}

// onError Callback 
//
function onError(error) {
    alert('code: '    + error.code    + '\n' + 
          'message: ' + error.message + '\n');
}

// Set audio position
// 
function setAudioPosition(position) {
    document.getElementById('audio_position').innerHTML = position;
}

//File upload

    function uploadAudio() {    

      var options = new FileUploadOptions();
        options.fileKey="file";
        options.fileName="test.mp3";
        options.mimeType="audio/mpeg";

        var ft = new FileTransfer();
        ft.upload("mnt/sdcard/" + audio_name, "http://www.richardsonweb.co.uk/fyp/test1/upload.php", win, fail, options);
    }

    function win() {
        alert("Yay! It worked!");
    }

    function 7(error) {
        alert("An error has occurred: Code = " = error.code);
    }

upload.php文件包含以下代码:

    print_r($_FILES);



$new_file_name = time() . ".mp3";
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/".$new_file_name);

但是,当我从FTP客户端下载文件时,Windows Media Player声称该文件已损坏。

奇怪的是,文件类型在VLC中播放正常(进度条在10秒内卡住,并且不会移动,但它会完美地播放音频)。

为什么会发生这种情况的任何想法?提前谢谢!

1 个答案:

答案 0 :(得分:2)

尝试使用此PHP代码

<?php
   $destination_path = getcwd().DIRECTORY_SEPARATOR;

   $result = 0;

   $target_path = $destination_path . time() . ".mp3";

   ini_set("upload_max_filesize", "3200000000");

   if(@move_uploaded_file($_FILES['file']['tmp_name'], $target_path)) {
      $result = 1;
   }

   sleep(1);
?>