Skip to content Skip to sidebar Skip to footer

Using Php To Save File With Unknown File Name

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 t

Solution 1:

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

Post a Comment for "Using Php To Save File With Unknown File Name"