douchun1900 2019-04-06 23:56
浏览 53

告诉文件的扩展名并回显某个值。 (PHP / jQuery的)

I've got a upload script that uses PHP (for upload), and jQuery for the progress bar. Here's the issue. I want it to only be allowed to have MP4s uploaded. It's working, I just need help to figure out how to fix the errors. See, if it isn't a mp4 it is supposed to echo Only MP4s are allowed at the time. elsewise, it will move the uploaded file to the users folder, then echo Done. Want to see it on <a href=channelname>your channel?</a>. Here's the issue. It won't echo out the errors. It just says "Done." whenever the file is done uploading. I would greatly appreciate help on how I can fix this.

Here's my code.

if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_FILES["userfile"])) {
    $file_type = $_FILES['userfile']['type']; //returns the mimetype

   $allowed = array("video/mp4");
   if(!in_array($file_type, $allowed)) {
     echo "Only MP4 videos are allowed to be uploaded because of security, and this is still a beta.";
     echo "<button onclick='window.location.reload()'>Upload a different file</button>";
     echo "<script>removeDummy()</script>";
   } else {
      if (!file_exists($dir)) {
         mkdir($dir, 0777, true);
      }

      move_uploaded_file($_FILES['userfile']['tmp_name'],"videos/".$user."/".$_FILES['userfile']['name']);

      echo "Done. Want to see it on <a href='channelname'>your channel</a>?";
    }
}

and here's the jQuery and JS

function toggleBarVisibility() {
    var e = document.getElementById("bar_blank");
    e.style.display = (e.style.display == "block") ? "none" : "block";
}

function createRequestObject() {
    var http;
    if (navigator.appName == "Microsoft Internet Explorer") {
        http = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else {
        http = new XMLHttpRequest();
    }
    return http;
}

function sendRequest() {
    var http = createRequestObject();
    http.open("GET", "progress.php");
    http.onreadystatechange = function () { handleResponse(http); };
    http.send(null);
}

function handleResponse(http) {
    var response;
    if (http.readyState == 4) {
        response = http.responseText;
        document.getElementById("bar_color").style.width = response + "%";
        document.getElementById("status").innerHTML = response + "%";

        if (response < 100) {
            setTimeout("sendRequest()", 1000);
        }
        else {
            toggleBarVisibility();
            document.getElementById("status").innerHTML = "Done.";
        }
    }
}

function startUpload() {
    toggleBarVisibility();
    setTimeout("sendRequest()", 1000);
}

(function () {
    document.getElementById("myForm").onsubmit = startUpload;
})();

When it gets done, it says "Done." like in the jQuery. I want it to echo like the PHP does, not from the jQuery.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 安卓adb backup备份应用数据失败
    • ¥15 eclipse运行项目时遇到的问题
    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)