Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Funkcje zwracanie return
matti9410
post
Post #1





Grupa: Zarejestrowani
Postów: 70
Pomógł: 0
Dołączył: 10.07.2013

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


Witam,
Dla wielu z was jest to pewnie banalne..
Mam funkcje przykładowo dodawania zdj
  1. function upload_images_file($images_file) {
  2. $id = $_SESSION['user_id'];
  3. $BASE_DIR = $_SERVER['DOCUMENT_ROOT'];
  4. $location_images_file = "$BASE_DIR/admin/images/user$id";
  5. $name_file = $images_file['name'];
  6. $type_file = $images_file['type'];
  7. $tmp_name_file = $images_file['tmp_name'];
  8. $size_file = $images_file['size'];
  9. $max_size_file = 2000000;
  10.  
  11. $info = pathinfo($name_file);
  12. $enlargement = $info['extension'];
  13.  
  14. if(($type_file == "image/jpeg") || ($type_file == "image/jpg") || ($type_file == "image/png")){
  15. if($size_file < $max_size_file){
  16. if (!file_exists($location_images_file))
  17. {
  18. mkdir("$location_images_file", 0777);
  19. if(is_uploaded_file($tmp_name_file)) {
  20. move_uploaded_file($tmp_name_file, "$location_images_file/avatar.$enlargement");
  21. return true;
  22. }else{
  23. return false;
  24. }
  25. }else{
  26. if(is_uploaded_file($tmp_name_file)) {
  27. move_uploaded_file($tmp_name_file, "$location_images_file/avatar.$enlargement");
  28. return true;
  29. }else{
  30. return false;
  31. }
  32. }
  33. }else{
  34. return false;
  35. }
  36. }else{
  37. return false;
  38. }
  39.  
  40. };


mam tam wiele if'ów i chciałbym się was zapytać skąd mam wiedzieć w którym ifie mi funkcja zwaraca true a w którym false. Jest mi to potrzebne bo np. chciałbym wyświetlać na tego podstawie komunikat czy np obraz ma zły format czy rozmiar.

Ten post edytował matti9410 22.04.2017, 16:09:37
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 7)
markonix
post
Post #2





Grupa: Zarejestrowani
Postów: 2 707
Pomógł: 290
Dołączył: 16.12.2008
Skąd: Śląsk

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


Zamiast false możesz zwracać coś innego, chociażby treść lub kod błędu.
Możesz też rzucać wyjątkiem (try catch).

Generalnie w dobrze napisanej aplikacji walidacja danych wejściowych przeprowadzana jest wcześniej jako osobny fragment. Potem już trzeba zostają jakieś naprawdę nietypowe sytuacje, których nie przewidzieliśmy (wtedy zazwyczaj wyświetlamy już coś w stylu "Nieoczekiwany błąd").

Ten post edytował markonix 22.04.2017, 16:30:47


--------------------
Go to the top of the page
+Quote Post
matti9410
post
Post #3





Grupa: Zarejestrowani
Postów: 70
Pomógł: 0
Dołączył: 10.07.2013

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


Czyli na false/true tego nie wykonam smile.gif
Zmieniłem kod na takie coś
  1. function upload_images_file($images_file){
  2. $id = $_SESSION['user_id'];
  3. $BASE_DIR = $_SERVER['DOCUMENT_ROOT'];
  4. $location_images_file = "$BASE_DIR/admin/images/user$id";
  5. $name_file = $images_file['name'];
  6. $type_file = $images_file['type'];
  7. $tmp_name_file = $images_file['tmp_name'];
  8. $size_file = $images_file['size'];
  9. $max_size_file = 2000000;
  10.  
  11. $info = pathinfo($name_file);
  12. $enlargement = $info['extension'];
  13.  
  14. if(($type_file == "image/jpeg") || ($type_file == "image/jpg") || ($type_file == "image/png")){
  15. if($size_file < $max_size_file){
  16. if(!file_exists($location_images_file)){
  17. mkdir("$location_images_file", 0777);
  18. if(is_uploaded_file($tmp_name_file)){
  19. move_uploaded_file($tmp_name_file, "$location_images_file/avatar.$enlargement");
  20. return true;
  21. }else{
  22. return "error3";
  23. }
  24. }else{
  25. if(is_uploaded_file($tmp_name_file)){
  26. move_uploaded_file($tmp_name_file, "$location_images_file/avatar.$enlargement");
  27. return true;
  28. }else{
  29. return "error3";
  30. }
  31. }
  32. }else{
  33. return "error2";
  34. }
  35. }else{
  36. return "error1";
  37. }
  38. };


oraz część wyświetlająca błędy
  1. if(isset($_POST["submit_avatar_user"])){
  2. if($_FILES["avatar_user"]["size"] == 0){
  3. $alert = '<div class="alert alert-warning" role="alert">
  4. <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
  5. <h2>Upsss...</h2>
  6. <p>Nie wybrano pliku do wysłania.</p>
  7. </div>';
  8. }else{
  9. if(upload_images_file($_FILES["avatar_user"]) == "error1"){
  10. $alert = '<div class="alert alert-warning" role="alert">
  11. <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
  12. <h2>Upsss...</h2>
  13. <p>Na serwer można wysłać tylko i wyłącznie pliki jpeg, jpg, png.</p>
  14. </div>';
  15. }else{
  16. if(upload_images_file($_FILES["avatar_user"]) == "error2"){
  17. $alert = '<div class="alert alert-warning" role="alert">
  18. <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
  19. <h2>Upsss...</h2>
  20. <p>Maksymalny rozmiar obrazu jaki można przesłać na serwer to 2MB.</p>
  21. </div>';
  22. }else{
  23. if(upload_images_file($_FILES["avatar_user"]) == "error3"){
  24. $alert = '<div class="alert alert-warning" role="alert">
  25. <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
  26. <h2>Upsss...</h2>
  27. <p>Niestety nie udało się zaaktualizować avatar.</p>
  28. </div>';
  29. }else{
  30. $alert = '<div class="alert alert-succes" role="alert">
  31. <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
  32. <h2>Zaaktualizowano !</h2>
  33. <p>Twój avatar został zaaktualizowany.</p>
  34. </div>';
  35. }
  36. }
  37. }
  38. }
  39. }


I nie potrafię sb poradzić ponieważ kod jak by zawiesza się na pierwszym if...

Edit

Dodam że wgrywam plik jpg.

Ten post edytował matti9410 22.04.2017, 16:44:52
Go to the top of the page
+Quote Post
markonix
post
Post #4





Grupa: Zarejestrowani
Postów: 2 707
Pomógł: 290
Dołączył: 16.12.2008
Skąd: Śląsk

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


Jak już nie masz zamiaru robić tego porządnie to chociaż tak..

if
elseif
elseif
elseif
else
-tu ostateczny kod


--------------------
Go to the top of the page
+Quote Post
Pyton_000
post
Post #5





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


A wiesz że tak jak masz teraz to w każdym warunku Twój kod próbuje zrobić upload pliku co za tym idzie kilkakrotnie wykonujesz bezsensowny kod...
Go to the top of the page
+Quote Post
matti9410
post
Post #6





Grupa: Zarejestrowani
Postów: 70
Pomógł: 0
Dołączył: 10.07.2013

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


Pyton_000 mógłbyś podrzucić pomysł jak tego uniknąć ?

markonix możesz pokazać przykład porządnego rozwiązania ?

Ten post edytował matti9410 22.04.2017, 17:55:20
Go to the top of the page
+Quote Post
markonix
post
Post #7





Grupa: Zarejestrowani
Postów: 2 707
Pomógł: 290
Dołączył: 16.12.2008
Skąd: Śląsk

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


https://www.google.pl/search?q=php+validati...alidation+class ale najlepiej jakiś framework, każdy ma walidacje już wbudowaną.

Ten post edytował markonix 22.04.2017, 18:07:28


--------------------
Go to the top of the page
+Quote Post
Pyton_000
post
Post #8





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


np:
  1. switch(upload_images_file($_FILES["avatar_user"])) {
  2. case "error1":
  3. // ...
  4. break;
  5. case 'error2':
  6. // ...
  7. break;
  8.  
  9. case true:
  10. // ...
  11. break;
  12.  
  13. default:
  14. // nieznane błędy
  15. break;
  16. }
Go to the top of the page
+Quote Post

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 Aktualny czas: 20.08.2025 - 23:46