PHP上传,提取和进度条 [英] PHP Upload, extract and progressbar

查看:113
本文介绍了PHP上传,提取和进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要帮助为我的php上传网站创建一个进度条。我有上传和exctract部分排序,但我需要进度条的帮助。我不知道该怎么做。另外,上传的文件大小是否有限?

HTML

 <?php if($ message)echo< p> $ message< / p>; ?> 
< form enctype =multipart / form-datamethod =postaction =>
< label>选择文件(.zip):< input type =filename =zip_file/>< / label>
< br />
< input type =submitvalue =Uploadname =submitvalue =Upload/>
< / form>

PHP
< ($ _ FILES [zip_file] [name]){
$ filename = $ _FILES [zip_file] [pre> < 名称];
$ 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',
'应用程序/ x压缩');

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

$ continue = strtolower($ name [1])=='zip'?真假;
if(!$ continue){
$ message =[...]不是.zip文件,请重试。;
}
$ 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 =您的.zip文件已上传并解压缩。;
} else {
$ message =上传出现问题,请重试。;
}
}
?>


解决方案

您可以进行一些修改,以及如果你想要一个进度条。你可以添加更多的eventlisteners,并使其成为你想要的。我希望这对你来说是一个很好的起点。

  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);
//定位您的php文件。
ajax.open(POST,upload.php);
ajax.send(formdata);
}
函数runprogress(event){
//进度%,你可能想要Math.round(百分比)
var percent =(event.loaded / event.total )* 100;
}
函数uploadcomplete(event){
//这将=您的php回复。
var AjaxReply = event.target.responseText;
}


I need help with creating a progress bar for my php upload site. I've got the upload and exctract part sorted but i need help with the progress bar. I'm not sure how to do it. Also, is there a maximum file size for the upload?

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.";
  }
}
?>

解决方案

You can make some changes to fit but this works rather well if you want a progress bar. You can add more eventlisteners and make it how you want. I hope this is a good starting point for you.

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;
}

这篇关于PHP上传,提取和进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆