Detailed Explanation of PHP Large File Block Upload Function Example

  • 2021-12-13 16:32:15
  • OfStack

This paper describes the block uploading function of PHP large files with examples. Share it for your reference, as follows:

Front end code

Use file. slice to split the files and upload them asynchronously.


<!DOCTYPE html>
<html lang="zh-cn">
 <head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title><%= title %></title>
  <!-- zui -->
  <link rel="stylesheet" href="http://zui.sexy/dist/css/zui.min.css" rel="external nofollow" >
 </head>
 <body>
  <div class="container" style="margin-top: 30px;">
    <form class="form-inline" method="post" enctype="multipart/form-data">
     <div class="form-group">
      <input type="file" id="fileBig" class="form-control">
     </div>
     <button type="submit" class="btn btn-primary"> Submit </button>
    </form>
  </div>
  <!-- ZUI Javascript  Dependency  jQuery -->
  <script src="http://zui.sexy/assets/jquery.js"></script>
  <!-- ZUI  Standard version compressed  JavaScript  Documents  -->
  <script src="http://zui.sexy/dist/js/zui.min.js"></script>
  <script type="text/javascript">
    $('form').submit(function() {
      let file = $(":file")[0].files[0];
      let fileName = file.name;
      let fileSize = file.size;
      console.log('fileSize',fileSize);
      let blockSize = 0.9*1024*1024;
      let num = Math.ceil(fileSize/blockSize);
      let start = 0;
      let end = 0;
      for(let i=1;i<=num;i++){
        end = blockSize*i;
        if(end > fileSize){
          end = fileSize;
        }
        let block = file.slice(start,end);
        start = end;
        let fd = new FormData();
        fd.append('block',block);
        fd.append('name',fileName);
        fd.append('total',num);
        fd.append('index',i);
        $.ajax({
          url:"upload.php",
          type:"POST",
          data:fd,
          async:true,
          processData:false,
          contentType:false,
          success:(res)=>{
            console.log('res_'+i+":");
            console.log(res);
          }
        })
      }
      return false;
    });
  </script>
 </body>
</html>

Back-end code

After all the files are uploaded successfully, merge to generate the original large file


<?php
$name = $_POST['name'];
$index = $_POST['index'];
$total = $_POST['total'];
echo "name:".$name.PHP_EOL;
echo "index:".$index.PHP_EOL;
echo "total:".$total.PHP_EOL;
move_uploaded_file($_FILES['block']['tmp_name'],'upload/'.$name."_".$index);
$list = scandir('upload');
$num = count($list)-2;
echo "cur_num:".$num.PHP_EOL;
if($num == $total){
  echo "upload done".PHP_EOL;
  echo $cmd = "cat upload/'{$name}_'* > upload/'{$name}'";
  shell_exec($cmd);
}

More readers interested in PHP can check the topics of this site: "php File Operation Summary", "PHP Directory Operation Skills Summary", "PHP Common Traversal Algorithms and Skills Summary", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary" and "PHP Network Programming Skills Summary"

I hope this article is helpful to everyone's PHP programming.


Related articles: