PHP Upload

    • VB.NET

    Es gibt 60 Antworten in diesem Thema. Der letzte Beitrag () ist von jonas16423.

      Rinecamo schrieb:

      //Dateityp und größe wird erst nach dem upload geprüft.
      if (($_FILES['file']['type'] == "image/gif")
      || ($_FILES['file']['type'] == "image/jpeg")
      || ($_FILES['file']['type'] == "image/pjpeg")
      && ($_FILES["file"]["size"] < 20000)) //Dateigröße angeben. Falls nicht gewollt einfach entfernen
      Wenn ich

      PHP-Quellcode

      1. $_FILES['file']['type']
      ausgebe, erhalte ich immer binary/octet-stream

      Die Abfrage funktioniert also nicht? Oder mach ich was falsch?

      Jedenfalls hier eine Variante vom Script welche Sicherheit bietet(Die Whitelist habe ich jetzt nicht drin)

      PHP-Quellcode

      1. <?php
      2. //error_reporting(-1);
      3. $UpDir = $_GET['updir'];
      4. $KOne = $_SERVER['HTTP_USER_AGENT'];
      5. $KMast = $_GET['zeHBDbxY'];
      6. $ReturnV = 'FAILED';
      7. $KOnee = 'ErstesPasswort';
      8. $KMaster = 'HierkönntihreinMasterPasswordfestlegen';
      9. // KOnee und KMaster sind eure beiden Passwörter, d.h. ihr könnt 2 beliebige Passwörter wählen
      10. if(isset($UpDir) && isset($KOne) && isset($KMast) && strlen($UpDir) > 2 && $KOne==$KOnee && $KMast==$KMaster)
      11. {
      12. if (is_uploaded_file($_FILES['file']['tmp_name']) && move_uploaded_file($_FILES['file']['tmp_name'], $UpDir.basename($_FILES['file']['name'])))
      13. {
      14. $ReturnV = "SUCCESS";
      15. }
      16. }
      17. echo $ReturnV;
      18. ?>



      Vb.Net Upload:

      VB.NET-Quellcode

      1. Dim Client As New WebClient
      2. With Client
      3. .Headers.Add("Content-Type", "binary/octet-stream")
      4. .Headers.Add("User-Agent", "DASERSTEPASSWORTHIERHIN")
      5. .UploadFileAsync(New Uri("Http://Webspace/Upload.php?&updir=./ORDNERNAME/&zeHBDbxY=HIERDASMASTERPW"), F)
      6. End With


      Falls ihr das Echo auslesen wollt dann anstatt UploadFileAsync, UploadFile Methode nehmen:

      VB.NET-Quellcode

      1. Dim Response As String = System.Text.Encoding.Default.GetString(Client.UploadFile("Http://Webspace/Upload.php?&updir=./ORDNERNAME/&zeHBDbxY=HIERDASMASTERPW", F))
      Umbenennen, die Datei wird ja erstmal in Temp Verzeichnis des Servers geladen und in PHP musst du ja mit move_uploaded_file(source, dest) die Datei dahin kopieren wo sie hinhaben willst, dabei kannst du ihr einen beliebigen Namen geben.
      @oliver14800
      Mit diesem Beispiel nicht, man müsste den Code etwas verändern, dann sollte das eigentlich relativ einfach sein (überprüfen ob Ordner existieren, falls nicht -> Ordner erstellen, u.s.w.)
      Ich werde das Skript und den VB-Code bei Gelegenheit nochmal überarbeiten.

      (Der Fehler liegt jedenfalls bei der Abfrage nach dem Typ der Datei, also einfach die oberste If-Abfrage im PHP-Skript entfernen)
      Komisch hat vor 2 Wochen alle funktioniert
      und jetzt mit dem beispiel Programm, der RICHTIGEN URL und der php Datei .. womit es schon funktionierte
      kommt immer der Fehler
      The remote server returned an error: (417) Expectation Failed

      EDIT:
      Hm... habe etwas rum probiert aber immer noch den selben Fehler

      Hab von allem mal ein Bild gemacht:


      Edit by LaMa5:
      - Bitte keine Doppelpost's, es gibt eine 'Bearbeiten' Funktion (Boardregeln §4.1e)
      --> Beiträge zusammengefügt.
      Bilder
      • Unbenannt.png

        282,83 kB, 1.600×858, 221 mal angesehen

      Dieser Beitrag wurde bereits 4 mal editiert, zuletzt von „oliver14800“ ()

      Hab mal eine Frage:
      Hab mir jetzt die "PHP-Upload.rar" gedownloadet, was muss ich jetzt am Script ändern? Ich habe die upload.php auf einen Server hochgeladen, hätte ich da dran noch was ändern müssen ? Am Code Vb-Code habe ich dann nur noch den Link geändert. Ich bekomme folgende Fehlermeldung: "Ausnahmefehler während eine WebCleint-Anforderung". Hilfe
      MFG VBWorld
      Mathe ist für alle die, die nicht mit dem Taschenrechner umgehen können :D
      Wie kann ich an dieser PHP Datei ein Bild Senden?

      PHP-Quellcode

      1. <?php
      2. //----------------------------------------- start edit here ---------------------------------------------//
      3. $script_location = "http:///pic/"; // location of the script
      4. $maxlimit = 9048576; // maxim image limit
      5. $folder = "images"; // folder where to save images
      6. // requirements
      7. $minwidth = 20; // minim width
      8. $minheight = 20; // minim height
      9. $maxwidth = 2560; // maxim width
      10. $maxheight = 1920; // maxim height
      11. //thumbnails - 1 or 0 to allow or disallow
      12. $thumb = 0; // allow to create thumb n.1
      13. $thumb2 = 0; // allow to create thumb n.2
      14. $thumb3 = 1; // allow to create thumb n.3
      15. // allowed extensions
      16. $extensions = array('.png', '.gif', '.jpg', '.jpeg','.PNG', '.GIF', '.JPG', '.JPEG');
      17. //----------------------------------------- end edit here ---------------------------------------------//
      18. // check that we have a file
      19. if((!empty($_FILES["uploadfile"])) && ($_FILES['uploadfile']['error'] == 0)) {
      20. // check extension
      21. $extension = strrchr($_FILES['uploadfile']['name'], '.');
      22. if (!in_array($extension, $extensions)) {
      23. echo 'wrong file format, alowed only .png , .gif, .jpg, .jpeg
      24. <script language="javascript" type="text/javascript">window.top.window.formEnable();</script>';
      25. } else {
      26. // get file size
      27. $filesize = $_FILES['uploadfile']['size'];
      28. // check filesize
      29. if($filesize > $maxlimit){
      30. echo "File size is too big.";
      31. } else if($filesize < 1){
      32. echo "File size is empty.";
      33. } else {
      34. // temporary file
      35. $uploadedfile = $_FILES['uploadfile']['tmp_name'];
      36. // capture the original size of the uploaded image
      37. list($width,$height) = getimagesize($uploadedfile);
      38. // check if image size is lower
      39. if($width < $minwidth || $height < $minheight){
      40. echo 'Image is to small. Required minimum '.$minwidth.'x'.$minheight.'
      41. <script language="javascript" type="text/javascript">window.top.window.formEnable();</script>';
      42. } else if($width > $maxwidth || $height > $maxheight){
      43. echo 'Image is to big. Required maximum '.$maxwidth.'x'.$maxheight.'
      44. <script language="javascript" type="text/javascript">window.top.window.formEnable();</script>';
      45. } else {
      46. // all characters lowercase
      47. $filename = strtolower($_FILES['uploadfile']['name']);
      48. // replace all spaces with _
      49. $filename = preg_replace('/\s/', '_', $filename);
      50. // extract filename and extension
      51. $pos = strrpos($filename, '.');
      52. $basename = substr($filename, 0, $pos);
      53. $ext = substr($filename, $pos+1);
      54. // get random number
      55. $rand = time();
      56. // image name
      57. $image = $basename .'-'. $rand . "." . $ext;
      58. // check if file exists
      59. $check = $folder . '/' . $image;
      60. if (file_exists($check)) {
      61. echo 'Image already exists';
      62. } else {
      63. // check if it's animate gif
      64. $frames = exec("identify -format '%n' ". $uploadedfile ."");
      65. if ($frames > 1) {
      66. // yes it's animate image
      67. // copy original image
      68. copy($_FILES['uploadfile']['tmp_name'], $folder . '/' . $image);
      69. // orignal image location
      70. $write_image = $folder . '/' . $image;
      71. //ennable form
      72. echo '<img src="' . $write_image . '" alt="'. $image .'" alt="'. $image .'" width="500" /><br />
      73. <input type="text" name="location" value="[IMG]'.$script_location.''.$write_image.'[/IMG]" class="location corners" />
      74. <script language="javascript" type="text/javascript">window.top.window.formEnable();</script>';
      75. } else {
      76. // create an image from it so we can do the resize
      77. switch($ext){
      78. case "gif":
      79. $src = imagecreatefromgif($uploadedfile);
      80. break;
      81. case "jpg":
      82. $src = imagecreatefromjpeg($uploadedfile);
      83. break;
      84. case "jpeg":
      85. $src = imagecreatefromjpeg($uploadedfile);
      86. break;
      87. case "png":
      88. $src = imagecreatefrompng($uploadedfile);
      89. break;
      90. }
      91. // copy original image
      92. copy($_FILES['uploadfile']['tmp_name'], $folder . '/' . $image);
      93. // orignal image location
      94. $write_image = $folder . '/' . $image;
      95. if ($thumb == 1){
      96. // create first thumbnail image - resize original to 80 width x 80 height pixels
      97. $newheight = ($height/$width)*80;
      98. $newwidth = 80;
      99. $tmp=imagecreatetruecolor($newwidth,$newheight);
      100. imagealphablending($tmp, false);
      101. imagesavealpha($tmp,true);
      102. $transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
      103. imagefilledrectangle($tmp, 0, 0, $newwidth, $newheight, $transparent);
      104. imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
      105. // write thumbnail to disk
      106. $write_thumbimage = $folder .'/thumb-'. $image;
      107. switch($ext){
      108. case "gif":
      109. imagegif($tmp,$write_thumbimage);
      110. break;
      111. case "jpg":
      112. imagejpeg($tmp,$write_thumbimage,100);
      113. break;
      114. case "jpeg":
      115. imagejpeg($tmp,$write_thumbimage,100);
      116. break;
      117. case "png":
      118. imagepng($tmp,$write_thumbimage);
      119. break;
      120. }
      121. }
      122. if ($thumb2 == 1){
      123. // create second thumbnail image - resize original to 125 width x 125 height pixels
      124. $newheight = ($height/$width)*125;
      125. $newwidth = 125;
      126. $tmp=imagecreatetruecolor($newwidth,$newheight);
      127. imagealphablending($tmp, false);
      128. imagesavealpha($tmp,true);
      129. $transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
      130. imagefilledrectangle($tmp, 0, 0, $newwidth, $newheight, $transparent);
      131. imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
      132. // write thumbnail to disk
      133. $write_thumb2image = $folder .'/thumb2-'. $image;
      134. switch($ext){
      135. case "gif":
      136. imagegif($tmp,$write_thumb2image);
      137. break;
      138. case "jpg":
      139. imagejpeg($tmp,$write_thumb2image,100);
      140. break;
      141. case "jpeg":
      142. imagejpeg($tmp,$write_thumb2image,100);
      143. break;
      144. case "png":
      145. imagepng($tmp,$write_thumb2image);
      146. break;
      147. }
      148. }
      149. if ($thumb3 == 1){
      150. // create third thumbnail image - resize original to 125 width x 125 height pixels
      151. $newheight = ($height/$width)*250;
      152. $newwidth = 250;
      153. $tmp=imagecreatetruecolor($newwidth,$newheight);
      154. imagealphablending($tmp, false);
      155. imagesavealpha($tmp,true);
      156. $transparent = imagecolorallocatealpha($tmp, 255, 255, 255, 127);
      157. imagefilledrectangle($tmp, 0, 0, $newwidth, $newheight, $transparent);
      158. imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
      159. // write thumbnail to disk
      160. $write_thumb3image = $folder .'/thumb3-'. $image;
      161. switch($ext){
      162. case "gif":
      163. imagegif($tmp,$write_thumb3image);
      164. break;
      165. case "jpg":
      166. imagejpeg($tmp,$write_thumb3image,100);
      167. break;
      168. case "jpeg":
      169. imagejpeg($tmp,$write_thumb3image,100);
      170. break;
      171. case "png":
      172. imagepng($tmp,$write_thumb3image);
      173. break;
      174. }
      175. }
      176. // all is done. clean temporary files
      177. imagedestroy($src);
      178. imagedestroy($tmp);
      179. // image preview
      180. if ($thumb == 1){
      181. echo "<img src='" . $write_thumbimage . "' alt='". $image ."' /><br />
      182. <input type='text' name='location' value='[IMG]".$script_location."". $write_thumbimage ."[/IMG]' class='location corners' /><br />
      183. <br />";
      184. }
      185. if ($thumb2 == 1){
      186. echo "<img src='" . $write_thumb2image . "' alt='". $image ."' /><br />
      187. <input type='text' name='location' value='[IMG]".$script_location."". $write_thumb2image ."[/IMG]' class='location corners' /><br />
      188. <br />";
      189. }
      190. if ($thumb3 == 1){
      191. echo "<img src='" . $write_thumb3image . "' alt='". $image ."' /><br />
      192. <input type='text' name='location' value='[IMG]".$script_location."". $write_thumb3image ."[/IMG]' class='location corners' /><br />
      193. <br />";
      194. }
      195. echo "<img src='" . $write_image . "' alt='". $image ."' alt='". $image ."' width='500' /><br />
      196. <input type='text' name='location' value='[IMG]".$script_location."".$write_image."[/IMG]' class='location corners' />
      197. <script language='javascript' type='text/javascript'>window.top.window.formEnable();</script>
      198. <div class='clear'></div>";
      199. }
      200. }
      201. }
      202. }
      203. // database connection
      204. include('inc/db.inc.php');
      205. // insert into mysql database and show success message
      206. mysql_query("INSERT INTO `image_upload` (`id`, `image`, `thumbnail`, `thumbnail2`, `thumbnail3` ) VALUES (NULL, '". $image ."', 'thumb-". $image ."', 'thumb2-". $image ."', 'thumb3-". $image ."')");
      207. }
      208. // error all fileds must be filled
      209. } else {
      210. echo '<div class="wrong">You must to fill all fields!</div>'; }
      211. ?>