I'm trying to record videos on browsers of mobile devices and send that videos to my PHP server. But when I inspect/debug my code in PHP the array $_FILES is empty. I'm sure that something is wrong in my code of JavaScript because of my lack of knowledge.
Here is my HTML / Javascript code :
<body>
<header>
<h1>HTML5 Video & Audio Input</h1>
<h2>Capturing Media with HTML and passing the data to PHP</h2>
</header>
<form method="post" action="serverTest.php" id="myform" enctype="multipart/form-data">
<label for="capture">Capture Media</label>
<input name= "uploadedVideo" type="file" id="videograbado" accept="video/*" capture="user-scalable" multiple />
<video id="player" controls></video>
</form>
<script>
document.addEventListener('DOMContentLoaded', (ev) => {
let form = document.getElementById('myform');
//get the captured media file
let input = document.getElementById('videograbado');
input.addEventListener('change', (ev) => {
console.dir(input.files[0]);
if (input.files[0].type.indexOf("video/") > -1) {
let video = document.getElementById('video');
var video1 = input.files[0];
video = window.URL.createObjectURL(input.files[0]);
var formData = new FormData();
formData.append('video-filename', input.files[0].name);
formData.append('video-blob', video);
xhr('serverTest.php', formData, function (fName) {
window.open(location.href + fName);
});
function xhr(url, data, callback) {
var request = new XMLHttpRequest();
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
callback(location.href + request.responseText);
}
};
request.open('post', url, true);
request.send(formData);
}
}
})
})
</script>
</body>
Any advice or suggestion are welcome.
Sources: https://www.youtube.com/watch?v=dbrez37HlJM
https://github.com/muaz-khan/RecordRTC/tree/master/RecordRTC-to-PHP