diff options
Diffstat (limited to 'articles/ajoutarticle.php')
| -rw-r--r-- | articles/ajoutarticle.php | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/articles/ajoutarticle.php b/articles/ajoutarticle.php new file mode 100644 index 0000000..56f63a1 --- /dev/null +++ b/articles/ajoutarticle.php | |||
| @@ -0,0 +1,103 @@ | |||
| 1 | <?php | ||
| 2 | // PAGE SÉCURISÉE ! impossible de la voir si la variable $_SESSION['mdpvalide'] ne vaut pas 1 | ||
| 3 | |||
| 4 | session_start(); | ||
| 5 | if (isset ($_SESSION["mdpvalide"])) | ||
| 6 | { | ||
| 7 | if($_SESSION["mdpvalide"]) // booléen, test si ça vaut 1 | ||
| 8 | {} // dans ce cas on fait rien et on laisse php lire la suite | ||
| 9 | else | ||
| 10 | { | ||
| 11 | header('Location: ../connexion.php'); | ||
| 12 | exit (); | ||
| 13 | // exit() sert à interrompre php qui n'a alors surtout pas besoin d'aller jusqu'en bas de la page | ||
| 14 | } | ||
| 15 | } | ||
| 16 | else | ||
| 17 | { | ||
| 18 | header('Location: ../connexion.php'); | ||
| 19 | exit (); | ||
| 20 | } | ||
| 21 | |||
| 22 | |||
| 23 | // connexion à la base de données | ||
| 24 | include("../connexionbase.php"); | ||
| 25 | |||
| 26 | |||
| 27 | // récupérer les POST, complèter la base et uploader des photos | ||
| 28 | if (isset ($_FILES['photo']) AND $_FILES['photo']['error'] == 1) // fichier trop lourd | ||
| 29 | { | ||
| 30 | header('Location: ../administration.php?nouvarticle=2'); | ||
| 31 | exit (); | ||
| 32 | } | ||
| 33 | elseif ($_FILES['photo']['error'] == 0 AND $_POST['titre'] != '') // présence d'un titre | ||
| 34 | { | ||
| 35 | // extension du fichier | ||
| 36 | $infophoto = pathinfo ($_FILES['photo']['name']); // pathinfo renvoie un tableau, qui contient notamment l'extension du fichier | ||
| 37 | $extautorisées = array ('jpg', 'jpeg', 'png', 'gif', 'JPG', 'JPEG', 'PNG', 'GIF'); | ||
| 38 | if (in_array ($infophoto['extension'], $extautorisées)) // on compare l'extension dans le tableau avec les extensions qu'on a choisi | ||
| 39 | { | ||
| 40 | // on récupère la variable $nom | ||
| 41 | $nom = basename ($_FILES['photo']['name']); | ||
| 42 | |||
| 43 | // vérification et formatage du nom du fichier | ||
| 44 | // supprimer les accents, problème: la méthode avec la fonction strstr marche mal avec l'UTF-8, | ||
| 45 | // on préfèrera celle-ci qui utilise str_replace et qui marche mieux (mais pas avec les majuscules accentuées): | ||
| 46 | $nom = str_replace( | ||
| 47 | array('à','â','ä','á','ã','å','î','ï','ì','í','ô','ö','ò','ó','õ','ø','ù','û','ü','ú','é','è','ê','ë','ç','ÿ','ñ'), | ||
| 48 | array('a','a','a','a','a','a','i','i','i','i','o','o','o','o','o','o','u','u','u','u','e','e','e','e','c','y','n'), | ||
| 49 | $nom); | ||
| 50 | // minuscules | ||
| 51 | $nom = mb_strtolower($nom, 'UTF-8'); | ||
| 52 | $infophoto['extension'] = mb_strtolower($infophoto['extension'], 'UTF-8'); | ||
| 53 | // ce qui n'est pas une lettre ou un chiffre est remplacé par un tiret, y compris les espaces | ||
| 54 | $nom = preg_replace('/([^.a-z0-9]+)/i', '-', $nom); | ||
| 55 | |||
| 56 | // ajout article avec photo | ||
| 57 | if (move_uploaded_file ($_FILES['photo']['tmp_name'], '/var/www/lelionetlecolibri.ordipolo.fr/articles/' . $nom)) | ||
| 58 | // on doit pouvoir écrire dans le dossier d'arrivée (mettre le chemin absolu) et dans /tmp | ||
| 59 | // pour permettre à PHP l'écriture et la traversée du dossier, taper: sudo chmod 733 /var/www/dossier/qu'on/veut | ||
| 60 | { | ||
| 61 | // nom de la miniature | ||
| 62 | $mini_nom = "mini_" . $nom; | ||
| 63 | |||
| 64 | $requête = $base->prepare('INSERT INTO articles (nomarticle, contenu, nomphoto, miniature) VALUES (?, ?, ?, ?)'); | ||
| 65 | $requête->execute(array( | ||
| 66 | $_POST['titre'], | ||
| 67 | $_POST['article'], | ||
| 68 | $nom, | ||
| 69 | $mini_nom)); | ||
| 70 | |||
| 71 | // création de la miniature | ||
| 72 | include("miniatures.php"); | ||
| 73 | |||
| 74 | header('Location: ../administration.php?nouvarticle=5'); // succès, move_uploaded_file renvoie la valeur TRUE | ||
| 75 | exit (); | ||
| 76 | } | ||
| 77 | else // echec à l'enregistrement, la fonction renvoie FALSE | ||
| 78 | { | ||
| 79 | header('Location: ../administration.php?nouvarticle=4'); | ||
| 80 | exit (); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | else // mauvais format | ||
| 84 | { | ||
| 85 | header('Location: ../administration.php?nouvarticle=3'); | ||
| 86 | exit (); | ||
| 87 | } | ||
| 88 | } | ||
| 89 | elseif ($_POST['titre'] != '') // article sans photo | ||
| 90 | { | ||
| 91 | $requête = $base->prepare('INSERT INTO articles (nomarticle, contenu) VALUES (?, ?)'); | ||
| 92 | $requête->execute(array( | ||
| 93 | $_POST['titre'], | ||
| 94 | $_POST['article'])); | ||
| 95 | |||
| 96 | header('Location: ../administration.php?nouvarticle=5'); | ||
| 97 | exit (); | ||
| 98 | } | ||
| 99 | else // pas de titre | ||
| 100 | { | ||
| 101 | header('Location: ../administration.php?nouvarticle=1'); | ||
| 102 | } | ||
| 103 | ?> | ||
