Upload a picture with a php function -


it's first time i'm trying create function , can't figure out... i'm trying build function upload picture , return path, echo doesn't display andi don't have syntaxe error tho... don't understand?

i hope may see more clearer me.

function.inc.php:

function upload_photo( $photo_input_name, $photo_path, $photo_type1, $photo_type2, $photo_max_weight, $photo_max_height, $photo_max_width) {  if (isset($_files[$photo_input_name])) {      //upload de fichier     if(!empty($_files[$photo_input_name]['tmp_name']))     { //si n'est pas vide          //spécification du chemin d'enregistrement         $dossier = $photo_path;          if (exif_imagetype($_files[$photo_input_name]['tmp_name']) != $photo_type1 , exif_imagetype($_files[$photo_input_name]['tmp_name']) != $photo_type2)         { //si le format de l'image est différent de jpg ou png                                      $erreur = 'oups, extension non reconnu. les extensions autorisées sont '.$photo_type1.' '.$photo_type2.'.';         }          else          { //si l'image est un jpg ou png              //on défini le poid max et on appel le poid de l'image uploader             $max_weight = $photo_max_weight;             $weight = filesize($_files[$photo_input_name]['tmp_name']);              if ($weight > $max_weight)             { // si le poid de l'image est supérieur au poid max autorisé                                    unlink($_files[$photo_input_name]['tmp_name']);                  $erreur = 'oups, le fichier est trops volumineux, il ne doit pas depasser '.$photo_max_weight.' mo.';             }              else             { // si le poid de l'image est inférieur ou egal on continue                                     $max_height = $photo_max_height;                 $max_width = $photo_max_width;                 $photo_size = getimagesize($_files[$photo_input_name]['tmp_name']);                  if ($photo_size == false)                 { // si les informations récuperer par la fonction getimagesize ne sont pas valide,                      // le fichier n'est pas une image, on le supprime et affiche une erreur                     unlink($_files[$photo_input_name]['tmp_name']);                      $erreur = 'oups, il semble que le fichier ne soit pas valide.';                 }                  if ($photo_size[1] != $max_height , $photo_size[0] != $max_width)                 { // si les dimensions de l'image sont differentes de $photo_max_width/height,                   // on efface l'image uploader et on affiche une erreur                     unlink($_files[$photo_input_name]['tmp_name']);                     $erreur = 'oups, il semble que l\'image ne soit pas au bon format ('.$max_width.' x '.$max_height.' px).';                 }                  if (!isset($erreur))                 { // si il n'y aucune erreur on continue vers l'enregistrement                      if (is_file($dossier.$_files[$photo_input_name]['name']))                     {// si il y un fichier du même nom dans le dossier on lui ajoute un prefix                          $file_upload = rand (0, 15).'_'.$_files[$photo_input_name]['name'];                          $fichier = $file_upload;                     }                      else $file_upload = $_files[$photo_input_name]['name'];                      $fichier = $file_upload;                      if(move_uploaded_file($_files[$photo_input_name]['tmp_name'], $dossier . $fichier))                      { //si l'image est uploader                         echo $dossier.$fichier;                         return true;                             }                 }                  else                  { //si l'upload echoue                      $erreur = 'oups, la copie de la photo sur le serveur échoué';                  }              }          }      }  }  } 

and page i'm trying use function:

if (isset($_files))  {        include('function.inc.php');      $photo_input_name = 'photo_buste_coeurg_'.$line;     $photo_path = '../../images/uploaded/';     $photo_type1 = 'imagetype_jpeg';     $photo_type2 = 'imagetype_png';     $photo_max_weight = 1048576;     $photo_max_height = 480;     $photo_max_width = 480;       upload_photo($photo_input_name, $photo_path, $photo_type1, $photo_type2, $photo_max_weight, $photo_max_height, $photo_max_width);   } 

thank time.

the problem here

if ($photo_size[1] != $max_height , $photo_size[0] != $max_width) 

you allow photos exactly $max_height high , exactly $max_width wide. != should >. also, and should or (or ||, more commonly used)

also imagetype_jpeg , imagetype_png constants, should use them such, not put value in string; doesn't work.

while we're here, allow me comment on how function set up. there few structural problems in code.

  • you assigning variables value of other variables quite often, quite confusing , unnecessary.
  • you doing if ... ... else making code overly complex. if not, , return (or, better, throw exception).
  • you limiting 2 photo types. if ever want remove on of them? if ever want add one? need go through code , replace function calls. instead, use array can give arbitrary number of options.

for reference, how write function

// function called upload_photo, no need prepend photo each , every argument function upload_photo($input_name, $path, array $types, $max_weight, $max_height, $max_width) {     if (empty($_files[$photo_input_name]['tmp_name']))     {         throw new exception('no file uploaded');     }      $type = exif_imagetype($_files[$photo_input_name]['tmp_name']);     if (!in_array($type, $types))     { //si le format de l'image est différent de jpg ou png                                  throw new exception('oups, extension non reconnu. les extensions autorisées sont '.implode(',', $types).'.');     }      $weight = filesize($_files[$photo_input_name]['tmp_name']);     if ($weight > $max_weight)     { // si le poid de l'image est supérieur au poid max autorisé                            unlink($_files[$photo_input_name]['tmp_name']);          throw new exception('oups, le fichier est trops volumineux, il ne doit pas depasser '.($max_weight / 1024 / 1024).' mo.');     }      $photo_size = getimagesize($_files[$photo_input_name]['tmp_name']);     if ($photo_size == false)     { // si les informations récuperer par la fonction getimagesize ne sont pas valide,          // le fichier n'est pas une image, on le supprime et affiche une erreur         unlink($_files[$photo_input_name]['tmp_name']);          throw new exception('oups, il semble que le fichier ne soit pas valide.');     }     if ($photo_size[1] > $max_height || $photo_size[0] > $max_width)     { // si les dimensions de l'image sont differentes de $photo_max_width/height,       // on efface l'image uploader et on affiche une erreur         unlink($_files[$photo_input_name]['tmp_name']);         throw new exception('oups, il semble que l\'image ne soit pas au bon format ('.$max_width.' x '.$max_height.' px).');     }      if (file_exists($path.$_files[$photo_input_name]['name']))     {// si il y un fichier du même nom dans le dossier on lui ajoute un prefix         // fixme: if renamed file exists? -- left exercise reader         $fichier = rand (0, 15).'_'.$_files[$photo_input_name]['name'];     }     else     {         $fichier = $_files[$photo_input_name]['name'];     }      if(!move_uploaded_file($_files[$photo_input_name]['tmp_name'], $path . $fichier))      { //cannot move file         throw new exception('oups, la copie de la photo sur le serveur échoué');             }      return $dossier.$fichier; } 

and how call it

if (isset($_files))  {        include('function.inc.php');      $input_name = 'photo_buste_coeurg_'.$line;     $path = '../../images/uploaded/';     $types = array(imagetype_jpeg, imagetype_png);     $max_weight = 1048576;     $max_height = 480;     $max_width = 480;      try {         upload_photo($input_name, $path, $types, $max_weight, $max_height, $max_width);     } catch (exception $e) {         echo 'erreur: '.$e->getmessage();     } } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -