使用PHP保存文件名未知的文件 [英] Using PHP to save file with unknown file name

查看:43
本文介绍了使用PHP保存文件名未知的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进行一个在线实验(使用jsPsych库),其中参与者(每个人的脚本编号由脚本随机分配)将记录许多.wav文件.然后,我想使用包含参与者的代码号和与该记录关联的项目号的名称上载到服务器.每个参与者将创建类似36个不同的.wav短文件.

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.

它看起来像recorderjs和 recordermp3.js 是我需要在浏览器端录制音频的内容(请参见RecorderJS通过AJAX上传记录的Blob ),但是我很难找到创建PHP脚本所需的信息,该脚本将保存文件名未知的文件.

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.

以下是相关的javascript:

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();
}

这是到目前为止我对PHP的了解:

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);
?>

我的问题与保存WAV文件非常相似在Chrome浏览器中记录到服务器HTML5和getUserMedia-录制音频和一定时间后保存到Web服务器但不同之处在于,我不知道上传文件的文件名,并且我想使用PHP(主要是因为有人告诉我).我可以使用什么代替php脚本中的"fileToUpload",以使该脚本适用于发送给它的任何.wav文件?

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?

如果您还没有猜到我在过去一个月左右的时间里已经了解了有关javascript和PHP的所有知识,那么请对n00b保持冷静.我看过各种PHP教程和文档,但似乎并没有在这里找到我想要的东西.代码将不胜感激.

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.

推荐答案

首先,您应该添加一些检查以确保您不会被帖子淹没.做到这一点的最小方法是,确保POST来自相同的服务器IP(尽管有几种方法可以欺骗它):

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');
}

关于文件名问题:一次只上传一个文件,因此 $ _ FILES 仅包含一个可以通过 current()检索的元素.

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);

这篇关于使用PHP保存文件名未知的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆