tp5大文件上传

====== 配置php.ini ====
max_execution_time = 6000
max_input_time = 6000
post_max_size = 90000M
upload_max_filesize = 80000M
max_file_uploads = 200

======= html ===

<div id="uploader" class="wu-example">
	<!--用来存放文件信息-->
	<div id="thelist" class="uploader-list"></div>
	<div class="btns">
		<div id="picker">选择文件</div>
	</div>
</div>

====== js ========

function uploadModel() {
    // 创建上传
    var uploader = WebUploader.create({
        auto: true, // 选中文件后,自动上传
        swf: '/static/public/webupload/Uploader.swf',
        server: './upload',      // 服务端地址
        pick:'#picker',
        resize: false,
        chunked: true,            //开启分片上传
        chunkSize: 1024*1024*100,   //每一片的大小
        chunkRetry: 5,          // 如果遇到网络错误,重新上传次数
        threads: 1,               // [默认值:3] 上传并发数。允许同时最大上传进程数。
        fileNumLimit:500,
        fileSizeLimit:1024*1024*1024*10,
        fileSingleSizeLimit:1024*1024*1024*10,
        formData: {model_type:model_type}
    });
    // 当有文件被添加进队列的时候
    uploader.on( 'fileQueued', function( file ) {
        var $list = $('#thelist');
        $list.html('');
        $list.append( '<div id="' + file.id + '" class="item">' +
            '<h4 class="info">' + file.name + '</h4>' +
            '<p class="state">等待上传...</p>' +
            '</div>' );
    });
    // 文件上传过程中创建进度条实时显示。
    uploader.on( 'uploadProgress', function( file, percentage ) {
        var $li = $( '#'+file.id ),
            $percent = $li.find('.layui-progress .layui-progress-bar');
        // 避免重复创建
        if ( !$percent.length ) {
            $percent = $('<div class="layui-progress layui-progress-big" lay-showpercent="true" lay-filter="demo">' +
                '<div class="layui-progress-bar layui-bg-red" lay-percent="0%"></div>'+
                '</div>').appendTo( $li ).find('.layui-progress-bar');
        }
        $li.find('p.state').text('上传中');
        element.progress('demo', percentage * 100 + '%');
        /*$percent.css( 'width', percentage * 100 + '%' );
        $percent.css( 'lay-percent', percentage * 100 + '%' );*/
    });

    //模型上传成功
    uploader.on('uploadSuccess', function (file, response) {
        $( '#'+file.id ).find('p.state').text('合并和解压中...');
        $.ajax({
            url: "./saveFile",
            type: "post",
            data: {
                oldName:response.oldName,
                uploadPath:response.filePaht,
                extension:response.fileSuffixes,
                path:response.path
            },
            dataType: "json",
            success: function (res) {
                if(res.code==2){
                    $( '#'+file.id ).find('p.state').text('上传成功');
                    $('#resource_name').val(file.name);
                    attachment_id = res.id;
                    layer.msg(res.msg);
                }else{
                    $( '#'+file.id ).find('p.state').text('合并解压出错,请重新上传');
                }
            }
        });
    });

    uploader.on( 'uploadError', function( file ) {
        $( '#'+file.id ).find('p.state').text('上传出错');
    });
}


============ tp5  php =======

// 上传分片文件,合并分片文件
public function upload(){
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
        header("Cache-Control: no-store, no-cache, must-revalidate");
        header("Cache-Control: post-check=0, pre-check=0", false);
        header("Pragma: no-cache");
        if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { exit; }
        if ( !empty($_REQUEST[ 'debug' ]) ) {
            $random = rand(0, intval($_REQUEST[ 'debug' ]) );
            if ( $random === 0 ) {
                header("HTTP/1.0 500 Internal Server Error");
                exit;
            }
        }
        @set_time_limit(30 * 60);
        $path = 'E:\WebServer\Resources'; //文件路径
        $date_dir = date('Ymd');
        $targetDir = $path . '\\' . 'file_material_tmp' . '\\' . $date_dir;
        $uploadDir = $path . '\\' . 'file_material' . '\\' . $date_dir;
        $cleanupTargetDir = true; // Remove old files
        $maxFileAge = 10 * 3600; // Temp file age in seconds
        if (!is_dir($targetDir)) {
            mkdir($targetDir, 0777, true);
        }
        if (!is_dir($uploadDir)) {
            mkdir($uploadDir, 0777, true);
        }
        // Get a file name
        if (isset($_REQUEST["name"])) {
            $fileName = $_REQUEST["name"];
        } else if (!empty($_FILES)) {
            $fileName = $_FILES["file"]["name"];
        } else {
            $fileName = uniqid("file_");
        }
        $oldName = $fileName;
        $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
        $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
        $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 1;
        // Remove old temp files
        if ($cleanupTargetDir) {
            if (!is_dir($targetDir) || !$dir = opendir($targetDir)) {
                die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
            }
            while (($file = readdir($dir)) !== false) {
                $tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;
                // If temp file is current file proceed to the next
                if ($tmpfilePath == "{$filePath}_{$chunk}.part" || $tmpfilePath == "{$filePath}_{$chunk}.parttmp") {
                    continue;
                }
                // Remove temp file if it is older than the max age and is not the current file
                if (preg_match('/\.(part|parttmp)$/', $file) && (@filemtime($tmpfilePath) < time() - $maxFileAge)) {
                    @unlink($tmpfilePath);
                }
            }
            closedir($dir);
        }
        // Open temp file
        if (!$out = @fopen("{$filePath}_{$chunk}.parttmp", "wb")) {
            die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
        }
        if (!empty($_FILES)) {
            if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) {
                die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
            }
            // Read binary input stream and append it to temp file
            if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb")) {
                die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
            }
        } else {
            if (!$in = @fopen("php://input", "rb")) {
                die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
            }
        }
        while ($buff = fread($in, 4096)) {
            fwrite($out, $buff);
        }
        @fclose($out);
        @fclose($in);
        rename("{$filePath}_{$chunk}.parttmp", "{$filePath}_{$chunk}.part");
        $done = true;
        for( $index = 0; $index < $chunks; $index++ ) {
            if ( !file_exists("{$filePath}_{$index}.part") ) {
                $done = false;
                break;
            }
        }
        if ( $done ) {
            $pathInfo = pathinfo($fileName);
            $hashStr = substr(md5($pathInfo['basename']),8,16);
            $hashName = time() . $hashStr . '.' .$pathInfo['extension'];
            $uploadPath = $uploadDir . DIRECTORY_SEPARATOR .$hashName;
            if (!$out = @fopen($uploadPath, "wb")) {
                die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
            }
            if ( flock($out, LOCK_EX) ) {
                for( $index = 0; $index < $chunks; $index++ ) {
                    if (!$in = @fopen("{$filePath}_{$index}.part", "rb")) {
                        break;
                    }
                    while ($buff = fread($in, 4096)) {
                        fwrite($out, $buff);
                    }
                    @fclose($in);
                    @unlink("{$filePath}_{$index}.part");
                }
                flock($out, LOCK_UN);
            }
            @fclose($out);
            $response = [
                'success'=>true,
                'oldName'=>$oldName,
                'filePaht'=>$uploadPath,
                'fileSuffixes'=>$pathInfo['extension'],
                'path'=>$path
            ];
            return json($response);
        }
        die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');
    }
	
	// 保存上传的压缩包并解压
	 public function saveFile()
    {
        $param = input('post.');
        $oldName = $param['oldName'];
        $uploadPath = $param['uploadPath'];
        $extension = $param['extension'];
        $path = $param['path'];
        //写入到文件表
        $data = [];
        $data[''] = $oldName;
		...(自己根据需求存文件)
        
        $res['src'] = $uploadPath;
        $res['code'] = 2;
        // 记得打开php.ini里的com.allow_dcom = true
		// 电脑里安装的有winrar
        $obj = new \COM('WScript.Shell');
        //执行doc解压命令
        $file_path = $res['src'];
        $flag = $obj->run("winrar x $file_path $path",1,true);
        if($flag == 1){ // 0 是成功 1是失败
            return json(['code'=>-1,'msg'=>'解压文件失败']);
        }
        $res['id'] = Db::name('attachment')->insertGetId($data);
        return json($res);
    }

 

 
 
G
M
T
 
 
检测语言世界语中文简体中文繁体丹麦语乌克兰语乌兹别克语乌尔都语亚美尼亚语伊博语俄语保加利亚语信德语修纳语僧伽罗语克罗地亚语冰岛语加利西亚语加泰罗尼亚语匈牙利语南非祖鲁语卡纳达语卢森堡语印地语印尼巽他语印尼爪哇语印尼语古吉拉特语吉尔吉斯语哈萨克语土耳其语塔吉克语塞尔维亚语塞索托语夏威夷语威尔士语孟加拉语宿务语尼泊尔语巴斯克语布尔语(南非荷兰语)希伯来语希腊语库尔德语弗里西语德语意大利语意第绪语拉丁语拉脱维亚语挪威语捷克语斯洛伐克语斯洛文尼亚语斯瓦希里语旁遮普语日语普什图语格鲁吉亚语毛利语法语波兰语波斯尼亚语波斯语泰卢固语泰米尔语泰语海地克里奥尔语爱尔兰语爱沙尼亚语瑞典语白俄罗斯语科萨科西嘉语立陶宛语索马里语约鲁巴语缅甸语罗马尼亚语老挝语芬兰语苏格兰盖尔语苗语英语荷兰语菲律宾语萨摩亚语葡萄牙语蒙古语西班牙语豪萨语越南语阿塞拜疆语阿姆哈拉语阿尔巴尼亚语阿拉伯语韩语马其顿语马尔加什语马拉地语马拉雅拉姆语马来语马耳他语高棉语齐切瓦语
 
世界语中文简体中文繁体丹麦语乌克兰语乌兹别克语乌尔都语亚美尼亚语伊博语俄语保加利亚语信德语修纳语僧伽罗语克罗地亚语冰岛语加利西亚语加泰罗尼亚语匈牙利语南非祖鲁语卡纳达语卢森堡语印地语印尼巽他语印尼爪哇语印尼语古吉拉特语吉尔吉斯语哈萨克语土耳其语塔吉克语塞尔维亚语塞索托语夏威夷语威尔士语孟加拉语宿务语尼泊尔语巴斯克语布尔语(南非荷兰语)希伯来语希腊语库尔德语弗里西语德语意大利语意第绪语拉丁语拉脱维亚语挪威语捷克语斯洛伐克语斯洛文尼亚语斯瓦希里语旁遮普语日语普什图语格鲁吉亚语毛利语法语波兰语波斯尼亚语波斯语泰卢固语泰米尔语泰语海地克里奥尔语爱尔兰语爱沙尼亚语瑞典语白俄罗斯语科萨科西嘉语立陶宛语索马里语约鲁巴语缅甸语罗马尼亚语老挝语芬兰语苏格兰盖尔语苗语英语荷兰语菲律宾语萨摩亚语葡萄牙语蒙古语西班牙语豪萨语越南语阿塞拜疆语阿姆哈拉语阿尔巴尼亚语阿拉伯语韩语马其顿语马尔加什语马拉地语马拉雅拉姆语马来语马耳他语高棉语齐切瓦语
 
 
 
 
 
 
 
 
 
文本转语音功能仅限200个字符
 
 
选项 : 历史 : 反馈 : Donate关闭
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值