Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Zmienna w nazwie uploadowanego obrazka
Forum PHP.pl > Forum > Przedszkole
szymek001
Witam, znalazłem dosyć ciekawy skrypt uploadu obrazków, jednak nie mogę sobie poradzić z przypisanej zmiennej do nazwy pliku. Chciałbym aby nazwa wrzucanego pliku była równa ID, bądź nazwie użytkownika. W obecnej postaci skrypt nie zmienia nazw zdjęć.

Kod skryptu do uploadu wygląda tak:
  1. <?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?>
  2. <?php
  3. // upload the file
  4. if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) {
  5.  
  6. // file needs to be jpg,gif,bmp,x-png and 4 MB max
  7. if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/png") && ($_FILES["image_upload_box"]["size"] < 99999000000))
  8. {
  9.  
  10.  
  11. // if user chosed properly then scale down the image according to user preferances
  12.  
  13. $max_upload_width = 2000;
  14. $max_upload_height = 2000;
  15.  
  16.  
  17.  
  18. // if uploaded image was JPG/JPEG
  19. if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){
  20. $image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);
  21. }
  22. // if uploaded image was GIF
  23. if($_FILES["image_upload_box"]["type"] == "image/gif"){
  24. $image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]);
  25. }
  26. // BMP doesn't seem to be supported so remove it form above image type test (reject bmps)
  27. // if uploaded image was BMP
  28. if($_FILES["image_upload_box"]["type"] == "image/bmp"){
  29. $image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);
  30. }
  31. // if uploaded image was PNG
  32. if($_FILES["image_upload_box"]["type"] == "image/png"){
  33. $image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);
  34. }
  35.  
  36. $remote_file = "image_files/".$_FILES["image_upload_box"]["name"];
  37. imagejpeg($image_source,$remote_file,100);
  38. chmod($remote_file,0644);
  39.  
  40.  
  41.  
  42. // get width and height of original image
  43. list($image_width, $image_height) = getimagesize($remote_file);
  44.  
  45. if($image_width>$max_upload_width || $image_height >$max_upload_height){
  46. $proportions = $image_width/$image_height;
  47.  
  48. if($image_width>$image_height){
  49. $new_width = $max_upload_width;
  50. $new_height = round($max_upload_width/$proportions);
  51. }
  52. else{
  53. $new_height = $max_upload_height;
  54. $new_width = round($max_upload_height*$proportions);
  55. }
  56.  
  57.  
  58. $new_image = imagecreatetruecolor($new_width , $new_height);
  59. $image_source = imagecreatefromjpeg($remote_file);
  60.  
  61. imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
  62. imagejpeg($new_image,$remote_file,100);
  63.  
  64. imagedestroy($new_image);
  65. }
  66.  
  67. imagedestroy($image_source);
  68.  
  69.  
  70. header("Location: submit.php?upload_message=image uploaded&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]);
  71. }
  72. else{
  73. header("Location: submit.php?upload_message=make sure the file is jpg, gif or png and that is smaller than 4MB&upload_message_type=error");
  74. }
  75. }
  76. ?>
  77.  
  78. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  79. <html xmlns="http://www.w3.org/1999/xhtml">
  80. <head>
  81. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  82. <title>Image Upload with resize</title>
  83. <style type="text/css">
  84. <!--
  85. body,td,th {
  86. font-family: Arial, Helvetica, sans-serif;
  87. color: #333333;
  88. font-size: 12px;
  89. }
  90.  
  91. .upload_message_success {
  92. padding:4px;
  93. background-color:#009900;
  94. border:1px solid #006600;
  95. color:#FFFFFF;
  96. margin-top:10px;
  97. margin-bottom:10px;
  98. }
  99.  
  100. .upload_message_error {
  101. padding:4px;
  102. background-color:#CE0000;
  103. border:1px solid #990000;
  104. color:#FFFFFF;
  105. margin-top:10px;
  106. margin-bottom:10px;
  107. }
  108.  
  109. -->
  110. </style></head>
  111.  
  112. <body>
  113.  
  114. <h1 style="margin-bottom: 0px">Submit an image</h1>
  115.  
  116.  
  117. <?php if(isset($_REQUEST['upload_message'])){?>
  118. <div class="upload_message_<?php echo $_REQUEST['upload_message_type'];?>">
  119. <?php echo htmlentities($_REQUEST['upload_message']);?>
  120. </div>
  121. <?php }?>
  122.  
  123.  
  124. <form action="submit.php" method="post" enctype="multipart/form-data" name="image_upload_form" id="image_upload_form" style="margin-bottom:0px;">
  125. <label>Image file, maximum 4MB. it can be jpg, gif, png:</label><br />
  126. <input name="image_upload_box" type="file" id="image_upload_box" size="40" />
  127. <input type="submit" name="submit" value="Upload image" />
  128.  
  129. <br />
  130. <br />
  131.  
  132.  
  133. <p style="padding:5px; border:1px solid #EBEBEB; background-color:#FAFAFA;">
  134. <strong>Notes:</strong><br />
  135. The image will not be resized to this exact size; it will be scalled down so that neider width or height is larger than specified.<br />
  136. When uploading this script make sure you have a directory called &quot;image_files&quot; next to it and make that directory writable, permissions 777.<br />
  137. After you uploaded images and made tests on our server please <a href="delete_all_images.php">delete all uploaded images </a> :)<br />
  138. </p>
  139.  
  140.  
  141.  
  142. <input name="submitted_form" type="hidden" id="submitted_form" value="image_upload_form" />
  143. </form>
  144.  
  145.  
  146.  
  147.  
  148. <?php if(isset($_REQUEST['show_image']) and $_REQUEST['show_image']!=''){?>
  149. <p>
  150. <img src="image_files/<?php echo $_REQUEST['show_image'];?>" />
  151. </p>
  152. <?php }?>
  153.  
  154.  
  155.  
  156.  
  157. </body>
  158. </html>
  159.  
  160.  
motyl-pl
Zmieniasz
  1. $_FILES["image_upload_box"]["name"]

na id użytkownika : ) tzn. nadpisujesz.
szymek001
ok, działa niby - bo nie ma żadnego błędu chociaż - niepotrzebnie zmieniałem za dużo ;D
jednak jak dodać do każdego rozszerzenie, np. ".jpg" bo jeśli zmienię nazwe na ID.jpg to obrazek ma nazwe "idjpg" bez kropki. Przez co gdy np. klikniemy na pokaż obrazek przeglądarka chce go pobierać zamiast wyświetlać ;]

Edit:

poradziłem sobie, zamiast formatu wstawiłem zmienną i działa:
  1. .$id.$zmienna
nospor
Cytat
bo jeśli zmienię nazwe na ID.jpg to obrazek ma nazwe "idjpg" bez kropki.
To pokaz jak ty to zmieniasz. Zazwyczaj ten niedobry PHP sam z siebie nie zjada kropek.
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.