upload.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. die("Make sure that you enable some form of authentication before removing this line.");
  16. // Make sure file is not cached (as it happens for example on iOS devices)
  17. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  18. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  19. header("Cache-Control: no-store, no-cache, must-revalidate");
  20. header("Cache-Control: post-check=0, pre-check=0", false);
  21. header("Pragma: no-cache");
  22. /*
  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. */
  30. // 5 minutes execution time
  31. @set_time_limit(5 * 60);
  32. // Uncomment this one to fake upload time
  33. // usleep(5000);
  34. // Settings
  35. $targetDir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload";
  36. //$targetDir = 'uploads';
  37. $cleanupTargetDir = true; // Remove old files
  38. $maxFileAge = 5 * 3600; // Temp file age in seconds
  39. // Create target dir
  40. if (!file_exists($targetDir)) {
  41. @mkdir($targetDir);
  42. }
  43. // Get a file name
  44. if (isset($_REQUEST["name"])) {
  45. $fileName = $_REQUEST["name"];
  46. } elseif (!empty($_FILES)) {
  47. $fileName = $_FILES["file"]["name"];
  48. } else {
  49. $fileName = uniqid("file_");
  50. }
  51. $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
  52. // Chunking might be enabled
  53. $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
  54. $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;
  55. // Remove old temp files
  56. if ($cleanupTargetDir) {
  57. if (!is_dir($targetDir) || !$dir = opendir($targetDir)) {
  58. die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
  59. }
  60. while (($file = readdir($dir)) !== false) {
  61. $tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;
  62. // If temp file is current file proceed to the next
  63. if ($tmpfilePath == "{$filePath}.part") {
  64. continue;
  65. }
  66. // Remove temp file if it is older than the max age and is not the current file
  67. if (preg_match('/\.part$/', $file) && (filemtime($tmpfilePath) < time() - $maxFileAge)) {
  68. @unlink($tmpfilePath);
  69. }
  70. }
  71. closedir($dir);
  72. }
  73. // Open temp file
  74. if (!$out = @fopen("{$filePath}.part", $chunks ? "ab" : "wb")) {
  75. die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
  76. }
  77. if (!empty($_FILES)) {
  78. if ($_FILES["file"]["error"] || !is_uploaded_file($_FILES["file"]["tmp_name"])) {
  79. die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
  80. }
  81. // Read binary input stream and append it to temp file
  82. if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb")) {
  83. die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
  84. }
  85. } else {
  86. if (!$in = @fopen("php://input", "rb")) {
  87. die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
  88. }
  89. }
  90. while ($buff = fread($in, 4096)) {
  91. fwrite($out, $buff);
  92. }
  93. @fclose($out);
  94. @fclose($in);
  95. // Check if file has been uploaded
  96. if (!$chunks || $chunk == $chunks - 1) {
  97. // Strip the temp .part suffix off
  98. rename("{$filePath}.part", $filePath);
  99. }
  100. // Return Success JSON-RPC response
  101. die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');