js的pupload函数 function uploadFile() { var support_file_id = $(" #support_package_id").val(); var product_id = $("#parent").val(); var version_id = $("#productVersionId").val(); $("#uploader").plupload({ runtimes: 'html5,silverlight,flash,html4', url: '__MODULE__/PublicFile/commonPlupload', chunk_size: '2mb', rename: true, dragdrop: true, prevent_duplicates: true, multipart_params : { versionId:version_id,productId:product_id,id:support_file_id}, filters: { max_file_size: '0', }, views: { list: true, thumbs: true, // Show thumbs active: 'list' }, multi_selection: false,//是否可以在文件浏览对话框中选择多个文件,true为可以,false为不可以 // unique_names: true, flash_swf_url: '__PUBLIC__/plupload2/Moxie.swf', silverlight_xap_url: '__PUBLIC__/plupload2/Moxie.xap', init: { BeforeUpload: function (up, file) { // up.setOption('multipart_params', {'group' : upload_path_id}); $.ajax({ url: '__MODULE__/PublicFile/checkFilePartsComm', type: 'post', // data:{filename : file.name,group:upload_path_id}, data: {filename: file.name}, async: false, success: function (re) { if (re * 1) { var hasUpChunks = re * 1; file.loaded = hasUpChunks * 1024 * 1024; } } }); }, ChunkUploaded: function (up, file, info) { var re = $.parseJSON(info.response); if (re.error) { file.loaded = 0; file.destroy(); } }, FileUploaded: function (up, file, info) { console.log(up); //console.log('[ File ]' + info); // Called when file has finished uploading var fileEle = $('#' + file.id), actionClass, iconClass; var re = $.parseJSON(info.response); if (re.error) { actionClass = 'ui-state-error plupload_failed'; iconClass = 'ui-icon-alert'; fileEle.find('.plupload_file_name').first().append(' <i style="color:#a94442"><b>[Error]:</b>' + re.error.message + '</i>'); } else { actionClass = 'plupload_done'; iconClass = 'ui-icon-circle-check'; if (!$.isEmptyObject(re.result)) { // var new_type_html = ''; // $('.res-window').prepend(new_type_html); } fileEle.removeClass('plupload_done ui-state-default').addClass(actionClass).find('.plupload_action_icon').first().removeClass('ui-icon-circle-check').addClass(iconClass); var obj = re.result; console.log(obj.resourceid); console.log(obj.old_filename); $(" #support_package_id").val(obj.resourceid); $(" .supportFile").html(obj.old_filename); // up.removeFile(file); // up.destroy(); } }, UploadComplete: function (up, files) { // Called when all files are either uploaded or failed //console.log('--------------------------------'); /* $.each(files, function(i, f) { up.removeFile(f); }); */ }, Error: function (up, errors) { // console.log(files); // console.log(up); // Called when error occurs //console.log(errors); }, } }); }
<link rel="stylesheet" href="__PUBLIC__/plupload2/jquery.ui.plupload/flick/jquery-ui.css" type="text/css" /> <link rel="stylesheet" href="__PUBLIC__/plupload2/jquery.ui.plupload/css/jquery.ui.plupload.css" type="text/css" /
<script type="text/javascript" src="__PUBLIC__/plupload2/plupload.full.min.js"></script> <script type="text/javascript" src="__PUBLIC__/plupload2/jquery.ui.plupload/flick/jquery-ui.min.js"></script> <script type="text/javascript" src="__PUBLIC__/plupload2/jquery.ui.plupload/jquery.ui.plupload.js"></script> <script type="text/javascript" src="__PUBLIC__/plupload2/i18n/zh_CN.js"></script>
//素材库,大文件上传 //这个是有用的,正在使用 public function commonPlupload() { 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"); // 5 minutes execution time @set_time_limit(5 * 60); $cleanupTargetDir = true; // Remove old files $maxFileAge = 5 * 3600; // Temp file age in seconds // 参数过滤 // $group = I('post.group','3', 'intval'); // if ($group == 0) { // $group = 3; // } $file_id = I("post.id","","intval"); // 文件上传参数设置 $allowMaxSize =-1; // 处理上传文件保存路径 && 处理文件保存名称 $targetDir = $savePath = ''; $DocModel = M('Doc'); $Doclib = D('Doclib'); // $targetDir = $Doclib->get_dir_path($group,'create'); //根据产品id获取文件路径 $productId = I("post.productId"); $versionId = I("post.versionId"); $dir_info = D("Doclib")->get_dir_product_about($productId,$versionId); $targetDir = $dir_info['path']; // $targetDir = C('upload_big_filepath').'/upload/Product/SoftPack/'; $fileName = $fileExt = ''; if (isset($_REQUEST["name"])) { $fileName = $_REQUEST["name"]; } elseif (!empty($_FILES)) { $fileName = $_FILES["file"]["name"]; } else { $fileName = uniqid() . '_' . time(); } if (in_array(PHP_OS,array('WINNT','WIN32','Windows'))) { $filePath = utf8ToGbk($targetDir . $fileName); $targetDir = utf8ToGbk($targetDir); $fileName = utf8ToGbk($fileName); }else{ $filePath = $targetDir . $fileName; } // Create target dir if (!file_exists($targetDir)) { @mkdir($targetDir, 0777, true); } // Get a file name if (file_exists($filePath)) { die('{"jsonrpc" : "2.0", "error" : {"code": 776, "message": "已存在同名文件!"}, "id" : "id"}'); } // Chunking might be enabled $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0; $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0; // Remove old temp files if ($cleanupTargetDir) { if (!is_dir($targetDir) || !$dir = opendir($targetDir)) { die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "打开目录失败!"}, "id" : "id"}'); } while (($file = readdir($dir)) !== false) { $tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file; /* if (substr($file, -5) == '.part') { @unlink($tmpfilePath); } */ // If temp file is current file proceed to the next if ($tmpfilePath == "{$filePath}.part") { continue; } // Remove temp file if it is older than the max age and is not the current file if (preg_match('/\.part$/', $file) && (filemtime($tmpfilePath) < time() - $maxFileAge)) { @unlink($tmpfilePath); } } closedir($dir); } // Open temp file if (!$out = @fopen("{$filePath}.part", $chunks ? "ab" : "wb")) { die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "打开文件失败!"}, "id" : "id"}'); } if (!empty($_FILES)) { if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) { die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "上传失败!"}, "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": "打开文件失败!"}, "id" : "id"}'); } } else { if (!$in = @fopen("php://input", "rb")) { die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "打开文件失败!"}, "id" : "id"}'); } } $isWrote = 1; while ($buff = fread($in, 4096)) { if (fwrite($out, $buff) === false) { $isWrote = 0; break; } } // //临时文件库中记录 // $tmpMLModel = M('ResmanagerFilesTemp'); // $dbFileName = gbkToUtf8($fileName); // $fileInDB = $tmpMLModel->where(array('filename'=>$dbFileName,'private_station_uuid'=>session('private_uuid')))->find(); // if ($fileInDB) { // //更新临时文件记录 // $tmpMLModel->where(array('id'=>$fileInDB['id'],'private_station_uuid'=>$_SESSION['private_uuid']))->save(array('chunk'=>$chunk + 1, 'create_time'=>date('Y-m-d H:i:s'))); // } else { // //向临时表中新增临时文件记录 // $data = array(); // $data['file_uuid'] = generateUniqueID(); // $data['filename'] = $dbFileName;//原始文件名 // $data['filepath'] = 'Uploads/'.session('private_uuid').'/sucai/'.$dbFileName.'.part';//临时文件的路径 // $data['chunk'] = $chunk + 1; // $data['create_time'] = date('Y-m-d H:i:s'); // $data['private_station_uuid'] = session('private_uuid'); // $tmpMLModel->add($data); // } @fclose($out); @fclose($in); // Check if file has been uploaded if (!$chunks || $chunk == $chunks - 1) { // 下面的两行代码是用来定文件上传后文件名为随机名称。author hq // 由于上传文件的文件名随机有问题,故暂时注释。author hq $fileInfo = pathinfo($fileName); // $fileName = generateUniqueID().'.'.$fileInfo['extension']; // Strip the temp .part suffix off rename("{$filePath}.part", $filePath); // $filePath = $targetDir.$fileName; // if ($chunks) { // //文件上传完成后,从临时文件库中删除 // $tmpMLModel->where(array('filename'=>$dbFileName,'private_station_uuid'=>$_SESSION['private_uuid']))->delete(); // } $filePath = gbkToutf8($filePath); $fileName = gbkToutf8($fileName); $old_filename = $_FILES["file"]["name"]; $file_type = $_FILES["file"]["type"]; $now = time(); $data['doclib_id'] = $dir_info['doclib_id']; $data['file_name'] = $fileName; $data['createby'] = $data['modifyby'] = session('MM_uid'); $data['createtime'] = $data['modifytime'] = date('Y-m-d H:i:s', $now); $data['old_filename'] = $old_filename; $data['file_size'] = filesize(realpath($filePath)); $data['file_type'] = $file_type; $data['filepath'] = $filePath; if($file_id){ $DocModel->where(array('id'=>$file_id))->save($data); $oldFilePath = $targetDir.$fileName; $oldFilePath = iconv("utf-8", "gb2312", $oldFilePath); if(is_file($oldFilePath)){ @unlink($oldFilePath); } $addRe = $file_id; }else{ $addRe = $DocModel->add($data); } if ($addRe !== false) { // 成功后,将更新日志 D('Log')->create_log($addRe,'创建文件',2); $reInfo = array( 'stat' => 1, 'size' => $data['file_size'], 'title' => $fileName, 'url' =>$filePath, 'old_filename' => $data['old_filename'], 'size' => round(filesize($filePath)/1024/1024,2).'MB', 'resourceid'=> $addRe, ); } else { // 此处目前仅支持删除本地文件,远程其他待 if (file_exists($filePath)) { @unlink($filePath); } die('{"jsonrpc" : "2.0", "error" : {"code": 777, "message": "数据库写入错误!"}, "id" : "id"}'); } } // Return Success JSON-RPC response //die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}'); $returnArr = array('jsonrpc'=>'2.0', 'result'=>$reInfo, 'id'=>'id'); die(json_encode($returnArr)); } // 文件上传时,检验目标文件夹的同名文件 public function checkFilePartsComm() { $filename = I('post.filename', ''); if (!$filename) { echo 0; } $DocModel = M('Doc'); $hasUpChunks = $DocModel->where(array('filename'=>$filename))->find(); if ($hasUpChunks) { echo 1; } else { echo 0; } }