Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Problem z uploadem plików
deha21
post 4.06.2012, 10:18:50
Post #1





Grupa: Zarejestrowani
Postów: 544
Pomógł: 5
Dołączył: 18.08.2009

Ostrzeżenie: (0%)
-----


Mam 3 inputy do wgrywania obrazków. Zrobiłem przez javascript ograniczenie do 2MB. Próbując je wgrać na serwer nie zawsze się to udaje bo czasem gdy wgram 3 pliki po 2MB one w ogóle się nie dodają, co więcej inne dane z POST również nie są przesyłane.

Kod funkcji zmniejszania zdjęć:
  1. // Zmniejszanie do odpowiedniej wielkości
  2. function resize($filef, $new_width, $new_height, $thumb, $quality) {
  3. $info = getimagesize($filef);
  4. if ($info === false)
  5. return false;
  6. $img = null;
  7. $img = imagecreatefromjpeg($filef);
  8. $th = imagecreatetruecolor($new_width, $new_height);
  9.  
  10. imagecopyresampled($th, $img, 0, 0, 0, 0, $new_width, $new_height, $info[0], $info[1]);
  11. imagejpeg($th, $filef, $quality);
  12. imagedestroy($th);
  13. imagedestroy($img);
  14. return true;
  15.  
  16. }
  17.  
  18. //tworzenie miniaturki 80x80
  19. function resize2($filef, $new_width, $new_height, $thumb, $quality) {
  20. $info = getimagesize($filef);
  21. if ($info === false)
  22. return false;
  23. $img = null;
  24. $img = imagecreatefromjpeg($filef);
  25. $th = imagecreatetruecolor(80, 80);
  26.  
  27. imagecopyresampled($th, $img, 0, 0, 0, 0, $new_width, $new_height, $info[0], $info[1]);
  28. imagejpeg($th, $filef, $quality);
  29. imagedestroy($th);
  30. imagedestroy($img);
  31. return true;
  32.  
  33. }


Ogólny skrypt upload'u zdjęć:
  1. function adpic($id,$arg,$key,$position) {
  2.  
  3. $path="../files/images/".$id;
  4. if (!file_exists($path)) { mkdir($path); chmod($path, 0777); }
  5. $newimage=uniqid().".jpg";
  6. move_uploaded_file($_FILES[$arg]["tmp_name"][$key],$path."/".$newimage);
  7. $info = getimagesize($path."/".$newimage);
  8.  
  9. if ($info[0]>$info[1]) {
  10. if ($info[0]>625) {
  11. $height=(625/$info[0])*$info[1];
  12. resize($path."/".$newimage, 625, $height, $path."/".$newimage, 75);
  13. }
  14. } elseif ($info[0]<$info[1]) {
  15. if ($info[1]>450) {
  16. $width=(450*$info[0])/$info[1];
  17. resize($path."/".$newimage, $width, 450, $path."/".$newimage, 75);
  18. }
  19. }
  20.  
  21. if ($info[0]<=$info[1]) {
  22. copy($path."/".$newimage,$path."/thumb_".$newimage);
  23. $thumb_height = (80/$info[0])*$info[1];
  24. resize2($path."/thumb_".$newimage, 80, $thumb_height, $path."/thumb_".$newimage, 80);
  25. } elseif ($info[0]>$info[1]) {
  26. copy($path."/".$newimage,$path."/thumb_".$newimage);
  27. $thumb_width=(80*$info[0])/$info[1];
  28. resize2($path."/thumb_".$newimage, $thumb_width, 80, $path."/thumb_".$newimage, 80);
  29. }
  30.  
  31. $pic=mysql_query("INSERT INTO pics values ('','".$id."','".$newimage."','".$position."')");
  32.  
  33. }


Skrypt wywoływania funkcji:
  1. foreach ($_FILES["image"]["error"] as $key => $error) {
  2. $position=$position+1;
  3. if ($_FILES["image"]["name"][$key]!="") {
  4. adpic($id,image,$key,$position);
  5. }
  6. }


W przypadku gdy skrypt się sypie wyskakują błędy o niezidentyfikowanych zmiennych a także "Invalid argument supplied for foreach()" (pewnie ze względu, że te zmienne nie są przesyłane).


--------------------
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 19.07.2025 - 01:30