• 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);
        }
    }
    ?>
  • 相关阅读:
    76页智慧物流园区综合解决方案2022(附下载)
    2023年Java毕业设计选题推荐,1000道创新创意Java毕业设计题目推荐,避免踩坑
    js中隐式类型转换与toPrimitive
    命令行 yes no选择
    燃烧化学平衡判据
    jar包和war包的区别
    1069 The Black Hole of Numbers
    父类和子类
    10 | 多态
    qt 开发api文档地址
  • 原文地址:https://blog.csdn.net/qq_24946333/article/details/105406951