upload.php 1.67 KB
Newer Older
Okthane's avatar
Okthane committed
1
<?php
Okthane's avatar
p  
Okthane committed
2 3
 //dossier temporaitre pour les images
define('PATH_TO_TEMP', "temp");
4

5
// Vérifier si le formulaire a été soumis
Okthane's avatar
Okthane committed
6 7
if($_SERVER["REQUEST_METHOD"] == "POST")
{
8
    // Vérifie si le fichier a été uploadé sans erreur.
9 10

    //format de la réponse
MARQUE Pierre's avatar
MARQUE Pierre committed
11 12
    if(isset($_FILES["photo"]) && $_FILES["photo"]["error"] == 0)
    {
Okthane's avatar
Okthane committed
13
        $allowed = array(
Okthane's avatar
m  
Okthane committed
14 15
            "jpg" => "image/jpeg",
            "jpeg" => "image/jpeg", 
Okthane's avatar
Okthane committed
16
            "png" => "image/png",
Okthane's avatar
m  
Okthane committed
17 18
            "PNG" => "image/PNG",
            "JPG" => "image/JPG"
Okthane's avatar
Okthane committed
19
        );
Okthane's avatar
m  
Okthane committed
20
        $filename = htmlspecialchars(trim($_FILES["photo"]["name"]));// évite les noms de fichiers trop exotiques 
Okthane's avatar
Okthane committed
21
        $newfilename = $filename;
22 23
        $filetype = $_FILES["photo"]["type"];
        $filesize = $_FILES["photo"]["size"];
MARQUE Pierre's avatar
MARQUE Pierre committed
24
        $message = "";
Okthane's avatar
Okthane committed
25
        $error = true; 
Okthane's avatar
Okthane committed
26 27
        $errorSize = true;
        $errorMIME = true;
28 29 30

        // Vérifie l'extension du fichier
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
31
        if(array_key_exists($ext, $allowed))//die("Erreur : Veuillez sélectionner un format de fichier valide.");
MARQUE Pierre's avatar
MARQUE Pierre committed
32
        {
Okthane's avatar
Okthane committed
33 34
            // Vérifie le type MIME du fichier ATTENTION - VERIFIE SEULEMENT L'EXTENSION
            if(in_array($filetype, $allowed))
MARQUE Pierre's avatar
MARQUE Pierre committed
35
            {
Okthane's avatar
Okthane committed
36
                $errorMIME = false;
37 38
            }
        }
Okthane's avatar
Okthane committed
39 40 41 42
        $maxsize = 1 * 1024 * 1024;
        if(!($filename > $maxsize))
        {
            $errorSize = false;
43 44
        }
    }
Okthane's avatar
Okthane committed
45
    $error = $errorMIME&&$errorSize;
Okthane's avatar
Okthane committed
46
    $data = array(
Okthane's avatar
Okthane committed
47 48 49
        
        "errorMIME" => $errorMIME,
        "errorSize" => $errorSize,
Okthane's avatar
p  
Okthane committed
50
        "error" => $error,//<----- Prend la valeur true (erreur!) ou false (ouf!)
Okthane's avatar
Okthane committed
51

Okthane's avatar
Okthane committed
52 53
    );
    echo json_encode($data);
54
}
Okthane's avatar
Okthane committed
55
?>
Okthane's avatar
Okthane committed
56