• PHP 大文件分块上传 底层实现


    <?php
    class clsSliceUpload
    {
        private $_blockFile;       // 分块文件
        private $_blockIdx;       //  第几个文件块
        private $_blockTotal;     //  文件块总数
        private $_config ;
        private $_destination_path = '';//目的路径
        private $_tmp_upload_path = '';//临时路径
    
        /**
         *
         * @param $blockFile 分块文件
         * @param array $config 基础配置
         */
        public function __construct($blockFile,array $config = [])
        {
    
            $this->_blockFile = $blockFile;
            $this->_config    = $config;
            $this->init($this->_config);
            $this->moveFile();
        
        }
    
        /**
         * 基础初始化
         * @param $configs
         */
        private function init($configs){
            $this->_blockIdx   = $configs['blockIdx']; 
            $this->_blockTotal = $configs['blockTotal']; 
            $this->_tmp_upload_path = $configs['tmp_upload_path'];
            $this->_destination_path = $configs['destination_path']; //目的路径
        }
        //  移动文件
        private function moveFile()
        {
             move_uploaded_file($this->_blockFile, $this->_tmp_upload_path);
            // 保持存活状态
            $_SESSION['sess_time'] = time();
            //开始合并文件
            $this->mergeFile();
        }
    
        /**
         *
         */
        private function mergeFile(){
    
            $filepath = $this->_destination_path;
            $fp = fopen($filepath, 'ab');
            $blob = file_get_contents($this->_tmp_upload_path);
            fwrite($fp,$blob);
            fclose($fp);
            unlink($this->_tmp_upload_path);
    
        }
        
        public function api()
        {
            $data = [];
            if ($this->_blockIdx == $this->_blockTotal) {
                if (file_exists($this->_destination_path)) {
                    $data['file_path'] = $this->_destination_path;
                }
            } else {
                $data = [
                    'percent' =>number_format($this->_blockIdx*100/$this->_blockTotal,2),
                ];
            }
            echo clsFunction::json($data);
        }
    }
    ?>
  • 相关阅读:
    某马机房预约系统 C++项目(一)
    为什么不试试神奇的3407呢?
    【c#】关于web api发布
    2022.08.05_每日一题
    400电话呼叫中心-佳音通讯400电话
    新版TCGA不同癌种数据合并
    1. 带你玩转Java之Java基本概括
    使用 qrcode 生成二维码
    FFmpeg和SDL实现视频播放器之 ⌈音视频同步⌋
    如何使用IDE端通义灵码
  • 原文地址:https://blog.csdn.net/qq_24946333/article/details/105406951