1

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.

4

1 に答える 1

1

問題はここにあります

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

正確に $max_heightさと幅の写真のみを許可します。それらはする必要があります。また、should (または、より一般的に使用される ) $max_width!=>ANDOR||

またIMAGETYPE_JPEG、 とIMAGETYPE_PNGは定数です。その値を文字列に入れるのではなく、そのまま使用する必要があります。うまくいきません。

私たちがここにいる間、機能がどのように設定されているかについてコメントさせてください. コードに構造的な問題がいくつかあります。

  • 変数に他の変数の値を頻繁に割り当てていますが、これは非常に紛らわしく、不必要です。
  • if ... then ... else を常に実行しているため、コードが非常に複雑になっています。そうでない場合は、戻ります (または、例外をスローすることをお勧めします)。
  • 写真の種類を 2 つに制限しています。それらを削除したい場合はどうしますか?追加したい場合はどうしますか?すべてのコードを調べて、関数呼び出しを置き換える必要があります。代わりに、任意の数のオプションを指定できるように配列を使用してください。

参考までに、これは私があなたの関数を書く方法です

// the function is called upload_photo, no need to prepend photo to each and 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 a un fichier du même nom dans le dossier on lui ajoute un prefix
        // FIXME: What if the renamed file also already exists? -- left as an exercise for the 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 a échoué');        
    }

    return $dossier.$fichier;
}

そして、これはあなたがそれを呼び出す方法です

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();
    }
}
于 2013-05-18T21:47:02.737 に答える