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 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
    • ¥20 关于URL获取的参数,无法执行二选一查询
    • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
    • ¥15 marlin编译错误,如何解决?
    • ¥15 有偿四位数,节约算法和扫描算法
    • ¥15 VUE项目怎么运行,系统打不开
    • ¥50 pointpillars等目标检测算法怎么融合注意力机制
    • ¥20 Vs code Mac系统 PHP Debug调试环境配置
    • ¥60 大一项目课,微信小程序
    • ¥15 求视频摘要youtube和ovp数据集