fileupload.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. } elseif (!empty($_FILES)) {
  61. $fileName = $_FILES["file"]["name"];
  62. } else {
  63. $fileName = uniqid("file_");
  64. }
  65. $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
  66. $uploadPath = $uploadDir . DIRECTORY_SEPARATOR . $fileName;
  67. // Chunking might be enabled
  68. $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
  69. $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 1;
  70. // Remove old temp files
  71. if ($cleanupTargetDir) {
  72. if (!is_dir($targetDir) || !$dir = opendir($targetDir)) {
  73. die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
  74. }
  75. while (($file = readdir($dir)) !== false) {
  76. $tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;
  77. // If temp file is current file proceed to the next
  78. if ($tmpfilePath == "{$filePath}_{$chunk}.part" || $tmpfilePath == "{$filePath}_{$chunk}.parttmp") {
  79. continue;
  80. }
  81. // Remove temp file if it is older than the max age and is not the current file
  82. if (preg_match('/\.(part|parttmp)$/', $file) && (@filemtime($tmpfilePath) < time() - $maxFileAge)) {
  83. @unlink($tmpfilePath);
  84. }
  85. }
  86. closedir($dir);
  87. }
  88. // Open temp file
  89. if (!$out = @fopen("{$filePath}_{$chunk}.parttmp", "wb")) {
  90. die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
  91. }
  92. if (!empty($_FILES)) {
  93. if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) {
  94. die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
  95. }
  96. // Read binary input stream and append it to temp file
  97. if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb")) {
  98. die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
  99. }
  100. } else {
  101. if (!$in = @fopen("php://input", "rb")) {
  102. die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
  103. }
  104. }
  105. while ($buff = fread($in, 4096)) {
  106. fwrite($out, $buff);
  107. }
  108. @fclose($out);
  109. @fclose($in);
  110. rename("{$filePath}_{$chunk}.parttmp", "{$filePath}_{$chunk}.part");
  111. $index = 0;
  112. $done = true;
  113. for( $index = 0; $index < $chunks; $index++ ) {
  114. if ( !file_exists("{$filePath}_{$index}.part") ) {
  115. $done = false;
  116. break;
  117. }
  118. }
  119. if ( $done ) {
  120. if (!$out = @fopen($uploadPath, "wb")) {
  121. die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
  122. }
  123. if ( flock($out, LOCK_EX) ) {
  124. for( $index = 0; $index < $chunks; $index++ ) {
  125. if (!$in = @fopen("{$filePath}_{$index}.part", "rb")) {
  126. break;
  127. }
  128. while ($buff = fread($in, 4096)) {
  129. fwrite($out, $buff);
  130. }
  131. @fclose($in);
  132. @unlink("{$filePath}_{$index}.part");
  133. }
  134. flock($out, LOCK_UN);
  135. }
  136. @fclose($out);
  137. }
  138. // Return Success JSON-RPC response
  139. die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');