fileupload2.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * upload.php
  4. *
  5. * Copyright 2013, Moxiecode Systems AB
  6. * Released under GPL License.
  7. *
  8. * License: http://www.plupload.com/license
  9. * Contributing: http://www.plupload.com/contributing
  10. */
  11. #!! IMPORTANT:
  12. #!! this file is just an example, it doesn't incorporate any security checks and
  13. #!! is not recommended to be used in production environment as it is. Be sure to
  14. #!! revise it and customize to your needs.
  15. // Make sure file is not cached (as it happens for example on iOS devices)
  16. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  17. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  18. header("Cache-Control: no-store, no-cache, must-revalidate");
  19. header("Cache-Control: post-check=0, pre-check=0", false);
  20. header("Pragma: no-cache");
  21. // header("HTTP/1.0 500 Internal Server Error");
  22. // echo mymd5('upload/C程序设计语言.pdf'); die;
  23. // Support CORS
  24. // header("Access-Control-Allow-Origin: *");
  25. // other CORS headers if any...
  26. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  27. exit; // finish preflight CORS requests here
  28. }
  29. if ( !empty($_REQUEST[ 'debug' ]) ) {
  30. $random = rand(0, intval($_REQUEST[ 'debug' ]) );
  31. if ( $random === 0 ) {
  32. header("HTTP/1.0 500 Internal Server Error");
  33. exit;
  34. }
  35. }
  36. // 5 minutes execution time
  37. @set_time_limit(5 * 60);
  38. // Uncomment this one to fake upload time
  39. // usleep(5000);
  40. // Settings
  41. // $targetDir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload";
  42. $targetDir = 'upload_tmp';
  43. $uploadDir = 'upload';
  44. $cleanupTargetDir = true; // Remove old files
  45. $maxFileAge = 5 * 3600; // Temp file age in seconds
  46. // Create target dir
  47. if (!file_exists($targetDir)) {
  48. @mkdir($targetDir);
  49. }
  50. // Create target dir
  51. if (!file_exists($uploadDir)) {
  52. @mkdir($uploadDir);
  53. }
  54. // Get a file name
  55. if (isset($_REQUEST["name"])) {
  56. $fileName = $_REQUEST["name"];
  57. } elseif (!empty($_FILES)) {
  58. $fileName = $_FILES["file"]["name"];
  59. } else {
  60. $fileName = uniqid("file_");
  61. }
  62. $md5File = @file('md5list2.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  63. $md5File = $md5File ? $md5File : array();
  64. if (isset($_REQUEST["md5"]) && array_search($_REQUEST["md5"], $md5File ) !== FALSE ) {
  65. die('{"jsonrpc" : "2.0", "result" : null, "id" : "id", "exist": 1}');
  66. }
  67. $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
  68. $uploadPath = $uploadDir . DIRECTORY_SEPARATOR . $fileName;
  69. // Chunking might be enabled
  70. $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
  71. $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;
  72. // Remove old temp files
  73. if ($cleanupTargetDir) {
  74. if (!is_dir($targetDir) || !$dir = opendir($targetDir)) {
  75. die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
  76. }
  77. while (($file = readdir($dir)) !== false) {
  78. $tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;
  79. // If temp file is current file proceed to the next
  80. if ($tmpfilePath == "{$filePath}.part") {
  81. continue;
  82. }
  83. // Remove temp file if it is older than the max age and is not the current file
  84. if (preg_match('/\.part$/', $file) && (filemtime($tmpfilePath) < time() - $maxFileAge)) {
  85. @unlink($tmpfilePath);
  86. }
  87. }
  88. closedir($dir);
  89. }
  90. // Open temp file
  91. if (!$out = @fopen("{$filePath}.part", $chunks ? "ab" : "wb")) {
  92. die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
  93. }
  94. if (!empty($_FILES)) {
  95. if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) {
  96. die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
  97. }
  98. // Read binary input stream and append it to temp file
  99. if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb")) {
  100. die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
  101. }
  102. } else {
  103. if (!$in = @fopen("php://input", "rb")) {
  104. die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
  105. }
  106. }
  107. while ($buff = fread($in, 4096)) {
  108. fwrite($out, $buff);
  109. }
  110. @fclose($out);
  111. @fclose($in);
  112. // Check if file has been uploaded
  113. if (!$chunks || $chunk == $chunks - 1) {
  114. // Strip the temp .part suffix off
  115. rename("{$filePath}.part", $filePath);
  116. rename($filePath, $uploadPath);
  117. array_push($md5File, mymd5($uploadPath));
  118. $md5File = array_unique($md5File);
  119. file_put_contents('md5list2.txt', join($md5File, "\n"));
  120. }
  121. function mymd5( $file ) {
  122. $fragment = 65536;
  123. $rh = fopen($file, 'rb');
  124. $size = filesize($file);
  125. $part1 = fread( $rh, $fragment );
  126. fseek($rh, $size-$fragment);
  127. $part2 = fread( $rh, $fragment);
  128. fclose($rh);
  129. return md5( $part1.$part2 );
  130. }
  131. // Return Success JSON-RPC response
  132. die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');