Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Własna nazwa pliku w plupload
Forum PHP.pl > Forum > Przedszkole
mlody69
Witam, używam plupload, w configu wyłączyłem aby nadawało wrzuconemu plikowi randomową nazwę, chcialbym aby plik po wrzuceniu miał nazwę np md5($fileName) ale nie wiem w którym miejscu to ustawić bo dla mnei to jest dosyć zagmatwane.
Kod:
  1. $targetDir = 'files';
  2.  
  3. $cleanupTargetDir = true; // Remove old files
  4. $maxFileAge = 5 * 3600; // Temp file age in seconds
  5.  
  6. // 5 minutes execution time
  7. @set_time_limit(5 * 60);
  8.  
  9. // Uncomment this one to fake upload time
  10. // usleep(5000);
  11.  
  12. // Get parameters
  13. $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0;
  14. $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 0;
  15. $fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : '';
  16.  
  17. // Clean the fileName for security reasons
  18. $fileName = preg_replace('/[^\w\._]+/', '_', $fileName);
  19.  
  20.  
  21. // Make sure the fileName is unique but only if chunking is disabled
  22. if ($chunks < 2 && file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName)) {
  23. $ext = strrpos($fileName, '.');
  24. $fileName_a = substr($fileName, 0, $ext);
  25. $fileName_b = substr($fileName, $ext);
  26.  
  27. $count = 1;
  28. while (file_exists($targetDir . DIRECTORY_SEPARATOR . $fileName_a . '_' . $count . $fileName_b))
  29. $count++;
  30.  
  31. $fileName = $fileName_a . '_' . $count . $fileName_b;
  32. }
  33.  
  34. $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName;
  35.  
  36. // Create target dir
  37. if (!file_exists($targetDir))
  38. @mkdir($targetDir);
  39.  
  40. // Remove old temp files
  41. if ($cleanupTargetDir && is_dir($targetDir) && ($dir = opendir($targetDir))) {
  42. while (($file = readdir($dir)) !== false) {
  43. $tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file;
  44.  
  45. // Remove temp file if it is older than the max age and is not the current file
  46. if (preg_match('/\.part$/', $file) && (filemtime($tmpfilePath) < time() - $maxFileAge) && ($tmpfilePath != "{$filePath}.part")) {
  47. @unlink($tmpfilePath);
  48. }
  49. }
  50.  
  51. closedir($dir);
  52. } else
  53. die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}');
  54.  
  55.  
  56. // Look for the content type header
  57. if (isset($_SERVER["HTTP_CONTENT_TYPE"]))
  58. $contentType = $_SERVER["HTTP_CONTENT_TYPE"];
  59.  
  60. if (isset($_SERVER["CONTENT_TYPE"]))
  61. $contentType = $_SERVER["CONTENT_TYPE"];
  62.  
  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("{$filePath}.part", $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.  
  72. if ($in) {
  73. while ($buff = fread($in, 4096))
  74. fwrite($out, $buff);
  75. } else
  76. die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
  77. fclose($in);
  78. fclose($out);
  79. @unlink($_FILES['file']['tmp_name']);
  80. } else
  81. die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
  82. } else
  83. die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}');
  84. } else {
  85. // Open temp file
  86. $out = fopen("{$filePath}.part", $chunk == 0 ? "wb" : "ab");
  87. if ($out) {
  88. // Read binary input stream and append it to temp file
  89. $in = fopen("php://input", "rb");
  90.  
  91. if ($in) {
  92. while ($buff = fread($in, 4096))
  93. fwrite($out, $buff);
  94. } else
  95. die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}');
  96.  
  97. fclose($in);
  98. fclose($out);
  99. } else
  100. die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}');
  101. }
  102.  
  103. // Check if file has been uploaded
  104. if (!$chunks || $chunk == $chunks - 1) {
  105. // Strip the temp .part suffix off
  106. rename("{$filePath}.part", $filePath);
  107. }
  108.  
  109.  
  110. // Return JSON-RPC response
  111. die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}');
  112.  
  113. ?>
nospor
$fileName = isset($_REQUEST["name"]) ? $_REQUEST["name"] : '';
Czy nazwa zmiennej $fileName naprawde nie dala ci ani odrobiny do myslenia?
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.