donglengyuan6826 2017-08-10 18:32
浏览 91
已采纳

使用PHP保存具有未知文件名的文件

I'm working on an online experiment (using the jsPsych library) where participants (each with a code number randomly assigned by the script) will record a number of .wav files. I then want to upload to the server with names that include the participant's code number and the item number associated with that recording. Each participant will be creating something like 36 different short .wav files.

It looks like recorderjs and recordermp3.js are what I need to record the audio on the browser side (see RecorderJS uploading recorded blob via AJAX), but I'm having difficulty finding the information I need to create a PHP script that will save a file of unknown file name.

Here's the relevant javascript:

function stopRecording(subjectID, item_number) {
    recorder && recorder.stop();
    console.log('Stopped recording.');
    recorder && recorder.exportWAV(function(blob) {
        var xhr=new XMLHttpRequest();
        xhr.onload=function(e) {
            if(this.readyState === 4) {
                console.log("Server returned: ",e.target.responseText);
            }
        };
        var fd=new FormData();
        fd.append(subjectID + item_number + ".wav", blob);
        xhr.open("POST","upload_wav.php",true);
        xhr.send(fd);
    };
    recorder.clear();
}

And here's what I have so far for PHP:

<?php
    $target_dir = 'audio/';
    $target_file=$target_dir . basename[$_FILES["fileToUpload"]["name"];
    move_uploaded_file($_FILES[“fileToUpload”][“tmp_name”], $target_file);
    chmod($target_file,0755);
?>

My question is very similar to Saving WAV File Recorded in Chrome to Server and HTML5 & getUserMedia - Record Audio & Save to Web Server after Certain Time but different in that I don't know what the filename of the uploaded file will be and I want to use PHP (mostly because I was told to). What can I use instead of "fileToUpload" in the php script to get this script to work for any .wav file that is sent to it?

In case you haven't already guessed I have learned everything in know about javascript and PHP in the last month or so, so please be gentle with the n00b. I have looked in various PHP tutorials and documentations but just don't seem to be finding what I'm looking for there. Code would be most appreciated.

</div>
  • 写回答

1条回答 默认 最新

  • dongzuoyue6556 2017-08-10 21:56
    关注

    First of all, you should add some checks to make sure you won't get post flooded. The minimal way to do this would be by making sure the POST comes from the same server IP (though there are several ways to spoof that):

    if ($_SERVER['REMOTE_ADDR'] != $_SERVER['SERVER_ADDR']) {
        http_response_code(403);
        die('Forbidden');
    }
    

    Also, make sure you only execute this script when you actually receive a file>

    if (!$_FILES) {
        http_response_code(400);
        die('File missing');
    }
    

    As for your filename problem: You are only uploading one file at a time, so $_FILES will only have one element that you can retrieve with current().

    $target_dir = 'audio/';
    $file = current($_FILES);
    $target_file = $target_dir . basename($file['name']);
    move_uploaded_file($file['tmp_name'], $target_file);
    // why would you do this?
    chmod($target_file,0755);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!