【问题标题】:$_FILES empty when sending to PHP server发送到 PHP 服务器时 $_FILES 为空
【发布时间】:2021-08-06 09:48:37
【问题描述】:

我正在尝试在移动设备的浏览器上录制视频并将该视频发送到我的 PHP 服务器。但是当我在 PHP 中检查/调试我的代码时,数组 $_FILES 是空的。由于我缺乏知识,我确信我的 JavaScript 代码有问题。

这是我的 HTML / Javascript 代码:

<body>
    <header>
        <h1>HTML5 Video &amp; 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>

欢迎任何意见或建议。

来源: https://www.youtube.com/watch?v=dbrez37HlJM

https://github.com/muaz-khan/RecordRTC/tree/master/RecordRTC-to-PHP

【问题讨论】:

    标签: javascript php html forms


    【解决方案1】:

    您的 FormData 对象包含两件事:

    formData.append('video-filename', input.files[0].name);
    

    文件名,是一个字符串。

    formData.append('video-blob', video);
    

    video的值是createObjectURL的返回值,也是一个字符串。


    如果您想填充$_FILES,那么您需要上传一个文件

    formData.append('video', input.files[0]);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-22
    • 2016-10-06
    • 2012-06-13
    • 1970-01-01
    • 2013-12-16
    相关资源
    最近更新 更多