upload.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. * upload.php
  4. *
  5. * Copyright 2009, 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. // HTTP headers for no cache etc
  12. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  13. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  14. header("Cache-Control: no-store, no-cache, must-revalidate");
  15. header("Cache-Control: post-check=0, pre-check=0", false);
  16. header("Pragma: no-cache");
  17. // Settings
  18. //$targetDir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload";
  19. $targetDir = 'uploads/';
  20. //$cleanupTargetDir = false; // Remove old files
  21. //$maxFileAge = 60 * 60; // Temp file age in seconds
  22. // 5 minutes execution time
  23. @set_time_limit(5 * 60);
  24. // Uncomment this one to fake upload time
  25. // usleep(5000);
  26. // Get parameters
  27. $chunk = isset($_REQUEST["chunk"]) ? $_REQUEST["chunk"] : 0;
  28. $chunks = isset($_REQUEST["chunks"]) ? $_REQUEST["chunks"] : 0;
  29. $fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : '';
  30. // Clean the fileName for security reasons
  31. $fileName = preg_replace('/[^\w\._]+/', '', $fileName);
  32. // Make sure the fileName is unique but only if chunking is disabled
  33. if ($chunks < 2 && file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName)) {
  34. $ext = strrpos($fileName, '.');
  35. $fileName_a = substr($fileName, 0, $ext);
  36. $fileName_b = substr($fileName, $ext);
  37. $count = 1;
  38. while (file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName_a . '_' . $count . $fileName_b))
  39. $count++;
  40. $fileName = $fileName_a . '_' . $count . $fileName_b;
  41. }
  42. // Create target dir
  43. if (!file_exists($targetDir))
  44. @mkdir($targetDir);
  45. // Remove old temp files
  46. /* this doesn't really work by now
  47. if (is_dir($targetDir) && ($dir = opendir($targetDir))) {
  48. while (($file = readdir($dir)) !== false) {
  49. $filePath = $targetDir . DIRECTORY_SEPARATOR . $file;
  50. // Remove temp files if they are older than the max age
  51. if (preg_match('/\\.tmp$/', $file) && (filemtime($filePath) < time() - $maxFileAge))
  52. @unlink($filePath);
  53. }
  54. closedir($dir);
  55. } else
  56. die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
  57. */
  58. // Look for the content type header
  59. if (isset($_SERVER["HTTP_CONTENT_TYPE"]))
  60. $contentType = $_SERVER["HTTP_CONTENT_TYPE"];
  61. if (isset($_SERVER["CONTENT_TYPE"]))
  62. $contentType = $_SERVER["CONTENT_TYPE"];
  63. // Handle non multipart uploads older WebKit versions didn't support multipart in HTML5
  64. if (strpos($contentType, "multipart") !== false) {
  65. if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
  66. // Open temp file
  67. $out = fopen($targetDir . DIRECTORY_SEPARATOR . $fileName, $chunk == 0 ? "wb" : "ab");
  68. if ($out) {
  69. // Read binary input stream and append it to temp file
  70. $in = fopen($_FILES['file']['tmp_name'], "rb");
  71. if ($in) {
  72. while ($buff = fread($in, 4096))
  73. fwrite($out, $buff);
  74. } else
  75. die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
  76. fclose($in);
  77. fclose($out);
  78. @unlink($_FILES['file']['tmp_name']);
  79. } else
  80. die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
  81. } else
  82. die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
  83. } else {
  84. // Open temp file
  85. $out = fopen($targetDir . DIRECTORY_SEPARATOR . $fileName, $chunk == 0 ? "wb" : "ab");
  86. if ($out) {
  87. // Read binary input stream and append it to temp file
  88. $in = fopen("php://input", "rb");
  89. if ($in) {
  90. while ($buff = fread($in, 4096))
  91. fwrite($out, $buff);
  92. } else
  93. die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
  94. fclose($in);
  95. fclose($out);
  96. } else
  97. die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
  98. }
  99. // Return JSON-RPC response
  100. die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');
  101. ?>