fileupload.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. #!! 注意
  12. #!! 此文件只是个示例,不要用于真正的产品之中。
  13. #!! 不保证代码安全性。
  14. #!! IMPORTANT:
  15. #!! this file is just an example, it doesn't incorporate any security checks and
  16. #!! is not recommended to be used in production environment as it is. Be sure to
  17. #!! revise it and customize to your needs.
  18. // Make sure file is not cached (as it happens for example on iOS devices)
  19. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  20. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  21. header("Cache-Control: no-store, no-cache, must-revalidate");
  22. header("Cache-Control: post-check=0, pre-check=0", false);
  23. header("Pragma: no-cache");
  24. // Support CORS
  25. // header("Access-Control-Allow-Origin: *");
  26. // other CORS headers if any...
  27. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  28. exit; // finish preflight CORS requests here
  29. }
  30. if ( !empty($_REQUEST[ 'debug' ]) ) {
  31. $random = rand(0, intval($_REQUEST[ 'debug' ]) );
  32. if ( $random === 0 ) {
  33. header("HTTP/1.0 500 Internal Server Error");
  34. exit;
  35. }
  36. }
  37. // header("HTTP/1.0 500 Internal Server Error");
  38. // exit;
  39. // 5 minutes execution time
  40. @set_time_limit(5 * 60);
  41. // Uncomment this one to fake upload time
  42. // usleep(5000);
  43. // Settings
  44. // $targetDir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload";
  45. $targetDir = 'upload_tmp';
  46. $uploadDir = 'upload';
  47. $cleanupTargetDir = true; // Remove old files
  48. $maxFileAge = 5 * 3600; // Temp file age in seconds
  49. // Create target dir
  50. if (!file_exists($targetDir)) {
  51. @mkdir($targetDir);
  52. }
  53. // Create target dir
  54. if (!file_exists($uploadDir)) {
  55. @mkdir($uploadDir);
  56. }
  57. // Get a file name
  58. if (isset($_REQUEST["name"])) {
  59. $fileName = $_REQUEST["name"];
  60. print_r($_FILES);
  61. $date = substr(md5(time()), 0,8);
  62. $name = explode('.',$fileName);
  63. $newPath = $date.'.'.$name[1];
  64. } elseif (!empty($_FILES)) {
  65. $fileName = $_FILES["file"]["name"];
  66. } else {
  67. $fileName = uniqid("file_");
  68. }
  69. $filePath = $targetDir . DIRECTORY_SEPARATOR . $newPath;
  70. $uploadPath = $uploadDir . DIRECTORY_SEPARATOR . $newPath;
  71. // Chunking might be enabled
  72. $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
  73. $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 1;
  74. // Remove old temp files
  75. if ($cleanupTargetDir) {
  76. if (!is_dir($targetDir) || !$dir = opendir($targetDir)) {
  77. die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
  78. }
  79. while (($file = readdir($dir)) !== false) {
  80. $tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;
  81. // If temp file is current file proceed to the next
  82. if ($tmpfilePath == "{$filePath}_{$chunk}.part" || $tmpfilePath == "{$filePath}_{$chunk}.parttmp") {
  83. continue;
  84. }
  85. // Remove temp file if it is older than the max age and is not the current file
  86. if (preg_match('/\.(part|parttmp)$/', $file) && (@filemtime($tmpfilePath) < time() - $maxFileAge)) {
  87. @unlink($tmpfilePath);
  88. }
  89. }
  90. closedir($dir);
  91. }
  92. // Open temp file
  93. if (!$out = @fopen("{$filePath}_{$chunk}.parttmp", "wb")) {
  94. die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
  95. }
  96. if (!empty($_FILES)) {
  97. if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) {
  98. die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
  99. }
  100. // Read binary input stream and append it to temp file
  101. if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb")) {
  102. die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
  103. }
  104. } else {
  105. if (!$in = @fopen("php://input", "rb")) {
  106. die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
  107. }
  108. }
  109. while ($buff = fread($in, 4096)) {
  110. fwrite($out, $buff);
  111. }
  112. @fclose($out);
  113. @fclose($in);
  114. rename("{$filePath}_{$chunk}.parttmp", "{$filePath}_{$chunk}.part");
  115. $index = 0;
  116. $done = true;
  117. for( $index = 0; $index < $chunks; $index++ ) {
  118. if ( !file_exists("{$filePath}_{$index}.part") ) {
  119. $done = false;
  120. break;
  121. }
  122. }
  123. if ( $done ) {
  124. if (!$out = @fopen($uploadPath, "wb")) {
  125. die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
  126. }
  127. if ( flock($out, LOCK_EX) ) {
  128. for( $index = 0; $index < $chunks; $index++ ) {
  129. if (!$in = @fopen("{$filePath}_{$index}.part", "rb")) {
  130. break;
  131. }
  132. while ($buff = fread($in, 4096)) {
  133. fwrite($out, $buff);
  134. }
  135. @fclose($in);
  136. @unlink("{$filePath}_{$index}.part");
  137. }
  138. flock($out, LOCK_UN);
  139. }
  140. @fclose($out);
  141. }
  142. // Return Success JSON-RPC response
  143. //die('{"jsonrpc" : "2.0", "result" : $filePath, "id" : "id"}');
  144. echo json_encode(array(array("filePath"=>$filePath)));