Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [php]Multiple upload rozmiary
Forum PHP.pl > Forum > Przedszkole
fr33d0m
Hello, korzystam z gotowej i popularnej klasy "class.upload.php". Z dodawaniem pojedynczych zdjec razem ze zmiana rozmiaru na male i duze z podzialem na katalogi problemu nie mam, prezentuje sie to tak:
  1. //#####################################//
  2. // ---------- IMAGE UPLOAD ---------- //
  3. //#####################################//
  4.  
  5. // we create an instance of the class, giving as argument the PHP object
  6. // corresponding to the file field from the form
  7. // All the uploads are accessible from the PHP object $_FILES
  8. $handle = new Upload($_FILES['my_field'], 'pl_PL');
  9.  
  10. // then we check if the file has been uploaded properly
  11. // in its *temporary* location in the server (often, it is /tmp)
  12. if ($handle->uploaded) {
  13.  
  14. // yes, the file is on the server
  15. // below are some example settings which can be used if the uploaded file is an image.
  16. $handle->image_resize = true;
  17. $handle->image_ratio_y = false;
  18. $handle->image_ratio_x = false;
  19. $handle->image_x = 70;
  20. $handle->image_y = 70;
  21.  
  22. // now, we start the upload 'process'. That is, to copy the uploaded file
  23. // from its temporary location to the wanted location ../.../admin/cfg.php
  24. // It could be something like $handle->Process('/home/www/my_uploads/');
  25. $handle->Process('./zdj/');
  26.  
  27. // we check if everything went OK
  28. if ($handle->processed) {
  29. // everything was fine !
  30. echo '<fieldset>';
  31. echo ' <legend>file uploaded with success</legend>';
  32. echo ' <img src="./zdj/' . $handle->file_dst_name . '" />';
  33. $info = getimagesize($handle->file_dst_pathname);
  34. echo ' <p>' . $info['mime'] . ' &nbsp;-&nbsp; ' . $info[0] . ' x ' . $info[1] .' &nbsp;-&nbsp; ' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB</p>';
  35. echo ' male: <a href="./zdj/' . $handle->file_dst_name . '">' . $handle->file_dst_name . '</a><br/>';
  36. echo '</fieldset>';
  37. } else {
  38. // one error occured
  39. echo '<fieldset>';
  40. echo ' <legend>file not uploaded to the wanted location</legend>';
  41. echo ' Error: ' . $handle->error . '';
  42. echo '</fieldset>';
  43. }
  44.  
  45. // we now process the image a second time, with some other settings
  46. $handle->image_resize = true;
  47. $handle->image_ratio_y = false;
  48. $handle->image_ratio_x = false;
  49. $handle->image_x = 300;
  50. $handle->image_y = 250;
  51.  
  52. $handle->Process('./zdj/_duze/');
  53.  
  54. // we check if everything went OK
  55. if ($handle->processed) {
  56. // everything was fine !
  57. echo '<fieldset>';
  58. echo ' <legend>file uploaded with success</legend>';
  59. echo ' <img src="./zdj/_duze/' . $handle->file_dst_name . '" />';
  60. $info = getimagesize($handle->file_dst_pathname);
  61. echo ' <p>' . $info['mime'] . ' &nbsp;-&nbsp; ' . $info[0] . ' x ' . $info[1] .' &nbsp;-&nbsp; ' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB</p>';
  62. echo ' DUZE: <a href="./zdj/_duze/' . $handle->file_dst_name . '">' . $handle->file_dst_name . '</a><br/>';
  63. echo '</fieldset>';
  64. } else {
  65. // one error occured
  66. echo '<fieldset>';
  67. echo ' <legend>file not uploaded to the wanted location</legend>';
  68. echo ' Error: ' . $handle->error . '';
  69. echo '</fieldset>';
  70. }
  71.  
  72. // we delete the temporary files
  73. $handle-> Clean();
  74.  
  75. } else {
  76. // if we're here, the upload file failed for some reasons
  77. // i.e. the server didn't receive the file
  78. echo '<fieldset>';
  79. echo ' <legend>file not uploaded on the server</legend>';
  80. echo ' Error: ' . $handle->error . '';
  81. echo '</fieldset>';
  82. }


Ale! nie mogę pojąć w jaki sposób przerobić dodawanie zdjec w trybie multi (czyli 3zdjecia na raz) w sposób, aby również były zmieniane rozmiary na małe i duże z podziałem na katalogi, ktoś mądry kto zna się na PDO pomoże?

  1. // ---------- MULTIPLE UPLOADS ----------
  2.  
  3. // as it is multiple uploads, we will parse the $_FILES array to reorganize it into $files
  4. $files = array();
  5. foreach ($_FILES['my_field'] as $k => $l) {
  6. foreach ($l as $i => $v) {
  7. if (!array_key_exists($i, $files))
  8. $files[$i] = array();
  9. $files[$i][$k] = $v;
  10. }
  11. }
  12.  
  13. // now we can loop through $files, and feed each element to the class
  14. foreach ($files as $file) {
  15.  
  16. // we instanciate the class for each element of $file
  17. $handle = new Upload($file);
  18.  
  19. // then we check if the file has been uploaded properly
  20. // in its *temporary* location in the server (often, it is /tmp)
  21. if ($handle->uploaded) {
  22.  
  23. // now, we start the upload 'process'. That is, to copy the uploaded file
  24. // from its temporary location to the wanted location
  25. // It could be something like $handle->Process('/home/www/my_uploads/');
  26. $handle->Process($dir_dest);
  27.  
  28. // we check if everything went OK
  29. if ($handle->processed) {
  30. // everything was fine !
  31. echo '<fieldset>';
  32. echo ' <legend>file uploaded with success</legend>';
  33. echo ' <p>' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB</p>';
  34. echo ' link to the file just uploaded: <a href="'.$dir_pics.'/' . $handle->file_dst_name . '">' . $handle->file_dst_name . '</a>';
  35. echo '</fieldset>';
  36. } else {
  37. // one error occured
  38. echo '<fieldset>';
  39. echo ' <legend>file not uploaded to the wanted location</legend>';
  40. echo ' Error: ' . $handle->error . '';
  41. echo '</fieldset>';
  42. }
  43.  
  44. } else {
  45. // if we're here, the upload file failed for some reasons
  46. // i.e. the server didn't receive the file
  47. echo '<fieldset>';
  48. echo ' <legend>file not uploaded on the server</legend>';
  49. echo ' Error: ' . $handle->error . '';
  50. echo '</fieldset>';
  51. }
  52. }
  53.  
  54. }
Fifi209
Z ciekawości, co ma PDO do upload'u zdjęć? Poza tym nie dałeś klasy.
fr33d0m
Ma to, że ten skrypt uploadu zdjęć jest w PDO, a ja bardziej kumam czysty PHP, który niestety jest już na wymarciu. Widziałem i testowałem skrytpy uploadu kilku zdjęć w PHP, ale jeden był z roku 2004 a drugi z 2008 więc sobie odpuściłem. Nie mogę wkleić całości bo jest więcej opisów niż kodu i post jest za długi, jest to niestosowne bo to ja oczekuje pomocy, ale tu jest adres tej klasy z dużą ilością informacji verot.net/php_class_upload.htm
Za pomoc z góry dziękuję, na pewno przyznam pkt-pomógł i wstawię linka do strony w stopce mojego forum na 2 tygodnie (dziennie ~5k odwiedzin). Serio mi zależy a nie chcę korzystać z innych gotowców.

Fifi209
Cytat(fr33d0m @ 19.06.2011, 13:23:57 ) *
Ma to, że ten skrypt uploadu zdjęć jest w PDO, a ja bardziej kumam czysty PHP

PDO to biblioteka, z której korzystasz jak ze wszystkich innych dostępnych w PHP. Nie wiem co ma PDO do "czystego" PHP. ;]
Cytat(fr33d0m @ 19.06.2011, 13:23:57 ) *
który niestety jest już na wymarciu.

PHP na wymarciu? Jeszcze długo nie.
Cytat(fr33d0m @ 19.06.2011, 13:23:57 ) *
Serio mi zależy a nie chcę korzystać z innych gotowców.

Rzuciłem wzrokiem na ten kod, nowy to on się raczej nie wydaje, świadczy o tym sama "obiektowość" tej klasy. Stare "var", brak modyfikatorów dostępu, jednym słowem znajdź coś innego. wink.gif
fr33d0m
Fifi209, wymądrzanie się pozostaw dla innych - mądrzejszych, jeśli nie umiesz rozkminić prostego zdania. "Czysty PHP" znaczy bez bibliotek i to miałem na myśli, to po pierwsze, a po drugie, PDO ma tyle do PHP co po pierwsze <-. teraz też nie rozumiesz prawda?
PHP absolutnie NIE jest na wymarciu, tu chodziło mi bardziej o pisanie strukturalne - przestarzałe, obecnie można stwierdzić, że standardem jest pisanie kodu obiektowego - zaprzeczysz?
Za Twoją ostatnią uwagę (jedyną na temat) dziękuję, ale pozostanę przy tym kodzie. A żeby mój post nie podchodził pod błachą reanimacje tematu czy flame w Twoim kierunku, napisze że sam do tego doszedłem analizując krok po kroku każdą linie... rozwiązanie jest banalnie proste - może komuś kiedyś się przyda:
  1. // ---------- MULTIPLE UPLOADS ----------
  2.  
  3. // as it is multiple uploads, we will parse the $_FILES array to reorganize it into $files
  4. $files = array();
  5. foreach ($_FILES['my_field'] as $k => $l) {
  6. foreach ($l as $i => $v) {
  7. if (!array_key_exists($i, $files))
  8. $files[$i] = array();
  9. $files[$i][$k] = $v;
  10. }
  11. }
  12.  
  13. // now we can loop through $files, and feed each element to the class
  14. foreach ($files as $file) {
  15.  
  16. // we instanciate the class for each element of $file
  17. $handle = new Upload($file);
  18.  
  19. // then we check if the file has been uploaded properly
  20. // in its *temporary* location in the server (often, it is /tmp)
  21. if ($handle->uploaded) {
  22.  
  23.  
  24. //duze zdj
  25. $handle->image_resize = true;
  26. $handle->image_ratio_y = false;
  27. $handle->image_ratio_x = false;
  28. $handle->image_x = 700;
  29. $handle->image_y = 750;
  30. // zapis powyzszych parametrow do ./zdj/_duze/
  31. $handle->Process('./zdj/_duze/');
  32.  
  33. //male zdj
  34. $handle->image_resize = true;
  35. $handle->image_ratio_y = false;
  36. $handle->image_ratio_x = false;
  37. $handle->image_x = 370;
  38. $handle->image_y = 370;
  39. // zapis powyzszych parametrow do ./zdj/
  40. $handle->Process('./zdj/');
  41.  
  42.  
  43. // we check if everything went OK
  44. if ($handle->processed) {
  45. // everything was fine !
  46. echo '<fieldset>';
  47. echo ' <legend>file uploaded with success</legend>';
  48. echo ' <p>' . round(filesize($handle->file_dst_pathname)/256)/4 . 'KB</p>';
  49. echo ' link to the file just uploaded: <a href="'.$dir_pics.'/' . $handle->file_dst_name . '">' . $handle->file_dst_name . '</a>';
  50. echo '</fieldset>';
  51. } else {
  52. // one error occured
  53. echo '<fieldset>';
  54. echo ' <legend>file not uploaded to the wanted location</legend>';
  55. echo ' Error: ' . $handle->error . '';
  56. echo '</fieldset>';
  57. }
  58.  
  59. } else {
  60. // if we're here, the upload file failed for some reasons
  61. // i.e. the server didn't receive the file
  62. echo '<fieldset>';
  63. echo ' <legend>file not uploaded on the server</legend>';
  64. echo ' Error: ' . $handle->error . '';
  65. echo '</fieldset>';
  66. }
  67. }
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.