Upon uploading a file from my web page, the dialog appears “No upload”, after the file uploads successfully. Any help with showing the correct message is appreciated.
<?php
foreach (array('video', 'audio') as $type) {
if (isset($_FILES["${type}-blob"])) {
$fileName = $_POST["${type}-filename"];
$uploadDirectory = 'uploads/' . $fileName;
// make sure that one can upload only allowed audio/video files
$allowed = array(
'webm',
'wav',
'mp4',
'mov'
);
$extension = pathinfo($uploadDirectory, PATHINFO_EXTENSION);
if (!$extension || empty($extension) || !in_array($extension, $allowed)) {
echo 'Invalid file extension: '.$extension;
return;
}
$new_filepath = "uploads/" . uniqid() . ".". $extension;
if (!move_uploaded_file($_FILES["${type}-blob"]["tmp_name"], $new_filepath)) {
echo (" problem moving uploaded file");
}
if(!file_exists($_FILES["${type}-blob"]["tmp_name"]) ||
!is_uploaded_file($_FILES["${type}-blob"]["tmp_name"])) {
echo 'No upload';
}
}
}
?>
And here’s the corresponding js:
function uploadFile() {
// create FormData
var fileType = 'video'; // or "audio"
var fileName = 'vid.webm';
var formData = new FormData();
var request = new XMLHttpRequest;
formData.append(fileType + '-filename', fileName);
formData.append(fileType + '-blob', blob);
request.open("POST", "/save1.php");
request.onreadystatechange = function() {
if(request.readyState==4) {
alert(request.responseText);
}
}
request.send(formData);
}