fileupload.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. // Support CORS
  22. // header("Access-Control-Allow-Origin: *");
  23. // other CORS headers if any...
  24. if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
  25. exit; // finish preflight CORS requests here
  26. }
  27. if ( !empty($_REQUEST[ 'debug' ]) ) {
  28. $random = rand(0, intval($_REQUEST[ 'debug' ]) );
  29. if ( $random === 0 ) {
  30. header("HTTP/1.0 500 Internal Server Error");
  31. exit;
  32. }
  33. }
  34. // 5 minutes execution time
  35. @set_time_limit(5 * 60);
  36. // Uncomment this one to fake upload time
  37. usleep(5000);
  38. // Settings
  39. // $targetDir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload";
  40. $targetDir = 'upload_tmp';
  41. $uploadDir = 'upload';
  42. $cleanupTargetDir = true; // Remove old files
  43. $maxFileAge = 5 * 3600; // Temp file age in seconds
  44. // Create target dir
  45. if (!file_exists($targetDir)) {
  46. @mkdir($targetDir);
  47. }
  48. // Create target dir
  49. if (!file_exists($uploadDir)) {
  50. @mkdir($uploadDir);
  51. }
  52. // Get a file name
  53. if (isset($_REQUEST["name"])) {
  54. $fileName = $_REQUEST["name"];
  55. } elseif (!empty($_FILES)) {
  56. $fileName = $_FILES["file"]["name"];
  57. } else {
  58. $fileName = uniqid("file_");
  59. }
  60. $md5File = @file('md5list.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  61. $md5File = $md5File ? $md5File : array();
  62. if (isset($_REQUEST["md5"]) && array_search($_REQUEST["md5"], $md5File ) !== FALSE ) {
  63. die('{"jsonrpc" : "2.0", "result" : null, "id" : "id", "exist": 1}');
  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. array_push($md5File, md5(file_get_contents($uploadPath)));
  138. $md5File = array_unique($md5File);
  139. file_put_contents('md5list.txt', join($md5File, "\n"));
  140. }
  141. // Return Success JSON-RPC response
  142. die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');