小编典典

PHP上传,提取和进度栏

html

我需要为我的php上传网站创建进度栏的帮助。我已经整理了上传和摘录部分,但我需要进度条的帮助。我不确定该怎么做。另外,上传文件的最大大小是多少?

的HTML

<?php if($message) echo "<p>$message</p>"; ?>
<form enctype="multipart/form-data" method="post" action="">
<label>Choose file (.zip): <input type="file" name="zip_file" /></label>
<br />
<input type="submit" value="Upload" name="submit" value="Upload" />
</form>

的PHP

<?php
if($_FILES["zip_file"]["name"]) {
  $filename = $_FILES["zip_file"]["name"];
  $source = $_FILES["zip_file"]["tmp_name"];
  $type = $_FILES["zip_file"]["type"];

  $name = explode(".", $filename);
  $accepted_types = array(
    'application/zip', 
    'application/x-zip-compressed', 
    'multipart/x-zip', 
    'application/x-compressed');

  foreach($accepted_types as $mime_type) {
    if($mime_type == $type) {
      $okay = true;
      break;
    } 
  }

  $continue = strtolower($name[1]) == 'zip' ? true : false;
  if(!$continue) {
    $message = "[...] not a .zip file. Please try again.";
  }
  $target_path = "./".$filename;
  if(move_uploaded_file($source, $target_path)) {
    $zip = new ZipArchive();
    $x = $zip->open($target_path);
    if ($x === true) {
      $zip->extractTo("./");
      $zip->close();

      unlink($target_path);
    }
    $message = "Your .zip file was uploaded and unpacked.";
  } else {  
    $message = "There was a problem with the upload. Please try again.";
  }
}
?>

阅读 400

收藏
2020-05-10

共1个答案

小编典典

您可以进行一些更改以适合需要,但是如果您需要进度条,则效果会很好。您可以添加更多事件监听器,并根据需要进行设置。希望这对您来说是一个很好的起点。

function uploadFile(){
    var file = document.getElementById("zip_file").files[0];
    var formdata = new FormData();
    formdata.append("zip_file", file);
    var ajax = new XMLHttpRequest();
    ajax.upload.addEventListener("progress", function(event) { runprogress(event); } , false);
    ajax.addEventListener("load", function(event) {uploadcomplete(event); }, false);
    //Target your php file. 
    ajax.open("POST", "upload.php");
    ajax.send(formdata);
}
function runprogress(event){
    //The progress %, you might want to Math.round(percent) 
    var percent = (event.loaded / event.total) * 100;
}
function uploadcomplete(event){
    //This will = to your php reply.
    var AjaxReply=event.target.responseText;
}
2020-05-10