PageRenderTime 28ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/examples/upload.php

https://github.com/roverwolf/plupload
PHP | 115 lines | 70 code | 20 blank | 25 comment | 22 complexity | eed1cba6cee4d997c48d7cc12cb86cac MD5 | raw file
  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('Content-type: text/plain; charset=UTF-8');
  13. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  14. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  15. header("Cache-Control: no-store, no-cache, must-revalidate");
  16. header("Cache-Control: post-check=0, pre-check=0", false);
  17. header("Pragma: no-cache");
  18. // Settings
  19. $targetDir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload";
  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. // usleep(5000);
  25. // Get parameters
  26. $chunk = isset($_REQUEST["chunk"]) ? $_REQUEST["chunk"] : 0;
  27. $chunks = isset($_REQUEST["chunks"]) ? $_REQUEST["chunks"] : 0;
  28. $fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : '';
  29. // Clean the fileName for security reasons
  30. $fileName = preg_replace('/[^\w\._]+/', '', $fileName);
  31. // Make sure the fileName is unique
  32. if (file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName)) {
  33. $ext = strrpos($fileName, '.');
  34. $fileName_a = substr($fileName, 0, $ext);
  35. $fileName_b = substr($fileName, $ext);
  36. $count = 1;
  37. while (file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName_a . '_' . $count . $fileName_b))
  38. $count++;
  39. $fileName = $fileName_a . '_' . $count . $fileName_b;
  40. }
  41. // Create target dir
  42. if (!file_exists($targetDir))
  43. @mkdir($targetDir);
  44. // Remove old temp files
  45. if (is_dir($targetDir) && ($dir = opendir($targetDir))) {
  46. while (($file = readdir($dir)) !== false) {
  47. $filePath = $targetDir . DIRECTORY_SEPARATOR . $file;
  48. // Remove temp files if they are older than the max age
  49. if (preg_match('/\\.tmp$/', $file) && (filemtime($filePath) < time() - $maxFileAge))
  50. @unlink($filePath);
  51. }
  52. closedir($dir);
  53. } else
  54. die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
  55. // Look for the content type header
  56. if (isset($_SERVER["HTTP_CONTENT_TYPE"]))
  57. $contentType = $_SERVER["HTTP_CONTENT_TYPE"];
  58. if (isset($_SERVER["CONTENT_TYPE"]))
  59. $contentType = $_SERVER["CONTENT_TYPE"];
  60. if (strpos($contentType, "multipart") !== false) {
  61. if (isset($_FILES['file']['tmp_name']) && is_uploaded_file($_FILES['file']['tmp_name'])) {
  62. // Open temp file
  63. $out = fopen($targetDir . DIRECTORY_SEPARATOR . $fileName, $chunk == 0 ? "wb" : "ab");
  64. if ($out) {
  65. // Read binary input stream and append it to temp file
  66. $in = fopen($_FILES['file']['tmp_name'], "rb");
  67. if ($in) {
  68. while ($buff = fread($in, 4096))
  69. fwrite($out, $buff);
  70. } else
  71. die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
  72. fclose($out);
  73. unlink($_FILES['file']['tmp_name']);
  74. } else
  75. die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
  76. } else
  77. die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
  78. } else {
  79. // Open temp file
  80. $out = fopen($targetDir . DIRECTORY_SEPARATOR . $fileName, $chunk == 0 ? "wb" : "ab");
  81. if ($out) {
  82. // Read binary input stream and append it to temp file
  83. $in = fopen("php://input", "rb");
  84. if ($in) {
  85. while ($buff = fread($in, 4096))
  86. fwrite($out, $buff);
  87. } else
  88. die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
  89. fclose($out);
  90. } else
  91. die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
  92. }
  93. // Return JSON-RPC response
  94. die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');
  95. ?>