LINUX教程:php结合web uploader插件实现分片上传文件

LINUX教程:php结合web uploader插件实现分片上传文件,第1张

概述介绍《LINUX教程:php结合web uploader插件实现分片上传文件》开发教程,希望对您有用。

《liNUX教程:PHP结合web uploader插件实现分片上传文件》要点:
本文介绍了liNUX教程:PHP结合web uploader插件实现分片上传文件,希望对您有用。如果有疑问,可以联系我们。

最近研究了下大文件上传的办法,找到了webuploader Js 插件进行大文件上传,大家也可以参考这篇文章进行学习:《Web Uploader文件上传插件使用详解》

使用

 使用webuploader分成简单直选要引入

<!--引入css--><link rel="stylesheet" type="text/CSS" href="webuploader文件夹/webuploader.CSS"><!--引入Js--><script type="text/JavaScript" src="webuploader文件夹/webuploader.Js"></script>

HTML部分

<div ID="uploader" > <!--用来存放文件信息--> <div ID="theList" ></div> <div >  <div ID="picker">选择文件</div>  <button ID="ctlBtn" >开始上传   </button> </div> </div>

初始化Web Uploader

jquery(function() {  $List = $('#theList'),$btn = $('#ctlBtn'),state = 'pending',uploader;  uploader = WebUploader.create({   // 不压缩image   resize: false,// swf文件路径   swf: 'uploader.swf',// 文件接收服务端.   server: upload.PHP,// 选择文件的按钮.可选.   // 内部根据当前运行是创建,可能是input元素,也可能是flash.   pick: '#picker',chunked: true,chunkSize:2*1024*1024,auto: true,accept: {    Title: 'Images',extensions: 'gif,jpg,jpeg,bmp,png',mimeTypes: 'image/*'   }  });

upload.PHP处理

以下是根据别人的例子自己拿来改的PHP 后台代码

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; // finish preflight CORS requests here  }  if ( !empty($_REQUEST[ 'deBUG' ]) ) {   $random = rand(0,intval($_REQUEST[ 'deBUG' ]) );   if ( $random === 0 ) {    header("http/1.0 500 Internal Server Error");    exit;   }  }  // header("http/1.0 500 Internal Server Error");  // exit;  // 5 minutes execution time  @set_time_limit(5 * 60);  // Uncomment this one to fake upload time  // usleep(5000);  // Settings  // $targetDir = ini_get("upload_tmp_dir") . DIRECTORY_SEParaTOR . "plupload";  $targetDir = 'uploads'.DIRECTORY_SEParaTOR.'file_material_tmp';  $uploadDir = 'uploads'.DIRECTORY_SEParaTOR.'file_material';  $cleanupTargetDir = true; // Remove old files  $maxfileAge = 5 * 3600; // Temp file age in seconds  // Create target dir  if (!file_exists($targetDir)) {   @mkdir($targetDir);  }  // Create target dir  if (!file_exists($uploadDir)) {   @mkdir($uploadDir);  }  // Get a file name  if (isset($_REQUEST["name"])) {   $filename = $_REQUEST["name"];  } elseif (!empty($_fileS)) {   $filename = $_fileS["file"]["name"];  } else {   $filename = uniqID("file_");  }  $oldname = $filename;  $filePath = $targetDir . DIRECTORY_SEParaTOR . $filename;  // $uploadpath = $uploadDir . DIRECTORY_SEParaTOR . $filename;  // Chunking might be enabled  $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","ID" : "ID"}');   }  }  while ($buff = fread($in,4096)) {   fwrite($out,$buff);  }  @fclose($out);  @fclose($in);  rename("{$filePath}_{$chunk}.parttmp","{$filePath}_{$chunk}.part");  $index = 0;  $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","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,'fileSize'=>$data['size'],'fileSuffixes'=>$pathInfo['extension'],'file_ID'=>$data['ID'],];   dIE(Json_encode($response));  }  // Return Success JsON-RPC response  dIE('{"Jsonrpc" : "2.0","result" : null,"ID" : "ID"}');

更多关于PHP文件上传的精彩内容请关注专题《PHP文件上传 *** 作汇总》,希望对大家有赞助.

以上就是本文的全部内容,希望对大家的学习有所赞助.

总结

以上是内存溢出为你收集整理的LINUX教程:php结合web uploader插件实现分片上传文件全部内容,希望文章能够帮你解决LINUX教程:php结合web uploader插件实现分片上传文件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/1042276.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存