It's the first time I'm trying to create a function and I can't figure it out... I'm trying to build a function who will upload a picture and return her path, but the echo doesn't display anything andI don't have any syntaxe error tho... I don't really understand?
I hope you may see more clearer than 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 AND 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 AND $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 a aucune erreur on continue vers l'enregistrement
if (is_file($dossier.$_FILES[$photo_input_name]['name']))
{// si il y a 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 a échoué';
}
}
}
}
}
}
And the page where I'm trying to use my 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 you for your time.