• 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);
        }
    }
    ?>
  • 相关阅读:
    Spring基础知识整理
    软考的时间安排
    教你实现物联网HMI/网关的趋势功能
    springboot中使用redis管理session
    解决svn update 产生Node remains in conflict的报错问题
    GUI-Guider软件使用
    多疑型性格的危害,如何改变多疑型性格?
    【无标题】
    NFT的价值 怎么玩NFT NFT定制开发
    大功率信号发生器原理是什么,安泰功率放大器型号推荐!
  • 原文地址:https://blog.csdn.net/qq_24946333/article/details/105406951