Ajax分割上传文件,无视php限制2M最大上传

<script src="http://lib.sinaapp.com/js/jquery/1.9.1/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
    $(function(){
        $('#btn').click(function() {
            sendfile();
        });
    })

     function sendfile() {
         const LENGTH = 1024 * 1024;
         var sta = 0;
         var end = sta + LENGTH;
         var blob = null;
         var fd = null;

         /*
             xhr 对象
         */
         var xhr = null;

         var mov = document.getElementsByName('mov')[0].files[0];
         //console.log(mov);return;

         var totalsize = mov.size;
         var percent = 0;

         // while(sta < totalsize) {
         timer = setInterval(function(){
            if (sta>totalsize) {
                clearInterval(timer);
            };
            blob = mov.slice(sta,end);
             fd = new FormData();
             fd.append('part',blob);

             xhr = new XMLHttpRequest();
             xhr.open('POST',"<?php echo site_url().'/admin/index/ajax' ?>",false);

             xhr.send(fd);

             sta = end;
             end = sta + LENGTH; 

             percent = 100 * end / totalsize;
             if(percent > 100) {
                 percent = 100;
             }
             // document.getElementById('bar').style.width = percent + '%';
             $('#bar').width(percent+'%');
             $('#bar').html(parseInt(percent)+'%');
         },1)

         // }

     }

</script>
HTML代码
<h1>html5大文件切割上传</h1>
<div id="progress"></div>
<input name="mov" type="file" />
<input id="btn" type="button" value="点我" />

"后台php代码"

public function ajax(){

if(!file_exists('./uploads/123.mp4')) {
     move_uploaded_file($_FILES['part']['tmp_name'],'./uploads/123.mp4');
} else {
     file_put_contents('./uploads/123.mp4',file_get_contents($_FILES['part']['tmp_name']),FILE_APPEND);
}

     echo 'ok';
}

php获取包含10w个手机号的txt文件(逗号隔开)

txt文件格式如下 13000000000,13000000005,13000000403,13001010129,13001010688,13001010838,13001016389,13001019538,13001030005,13001042700,13001047495...

阅读全文

php使用Carbon获取2个日期之间的日期

<?php use Carbon\CarbonPeriod; $period = CarbonPeriod::create('2018-06-14', '2018-06-20'); // Iterate over the period fore...

阅读全文

php 解决超卖的几种方案(redis锁、mysql悲观锁)

1.创建商品和订单表模拟商品交易操作 fa_goods商品表,stock商品库存,test_order=订单表 2.添加一个测试商品数据(商品数量为150个) 测试并发购买的方法(gol...

阅读全文

欢迎留言