Miniature PHP

Par leocoinsin, il y a 9 ans


Bonjour,

Je rencontre un probleme avec mon code PHP lors de la creation d'un systeme d'articles en PHP.

Je cherche simplement a introduire l'ajout d'un fichier type image JPG dans la creation d'un articles.
Voici la partie redaction lorsque je souhaite rediger un nouvel article.php

<?php $bdd = new PDO("mysql:host=localhost;dbname=flambeaux_site1;port=8888;charset=utf8","root","root"); $mode_edition = 0; if(isset($_GET['edit']) AND !empty($_GET['edit'])) { $mode_edition = 1; $edit_id = htmlspecialchars($_GET['edit']); $edit_article = $bdd -> prepare('SELECT * FROM articles WHERE id = ?'); $edit_article -> execute(array($edit_id)); if ($edit_article -> rowCount() == 1) { $edit_article = $edit_article -> fetch(); } else { die('Erreur : L\'article conecérné n\'existe pas !'); } } if (isset($_POST['titre_article'], $_POST['contenu_article'])) { if (!empty($_POST['titre_article']) AND (!empty($_POST['contenu_article']))) { $titre_article = htmlspecialchars($_POST['titre_article']); $contenu_article = htmlspecialchars($_POST['contenu_article']); if($mode_edition == 0) { var_dump($_FILES); var_dump(exif_imagetype($_FILES['miniature']['tmp_name'])); $ins = $bdd -> prepare('INSERT INTO articles (titre, contenu, date_time_publication) VALUES (?,?,NOW())'); $ins -> execute(array($titre_article, $contenu_article)); $lastid = $bdd->lastInsertId(); if(isset($_FILES['miniature']) AND !empty($_FILES['miniature']['name'])) { if(exif_imagetype($_FILES['miniature']['tmp_name']) == 2) { $chemin = 'miniatures/'.$lastid.'.jpg'; move_uploaded_file($_FILES['miniature']['tmp_name'], $chemin); } else { $message = 'Votre image doit être au format jpg'; } } $message = 'Votre article a bien etait crée'; } else { $update = $bdd -> prepare('UPDATE articles SET titre = ?, contenu = ?, date_time_edition = NOW() WHERE id = ?'); $update -> execute(array($titre_article, $contenu_article, $edit_id)); $message = 'Votre article a bien etait mise a jour'; } } else { $message = 'Veuillez remplir tous les champs'; } } ?> <!DOCTYPE html> <html> <head> <title>Redaction / Edition</title> </head> <body> <form method="POST" enctype="multipart/form-data"> <input type="text" placeholder="Titre" name="titre_article" <?php if($mode_edition == 1) { ?> value="<?= $edit_article['titre'] ?>"<?php } ?> /><br /> <textarea placeholder="contenu de l'article" name="contenu_article"><?php if($mode_edition == 1) { ?><?= $edit_article['contenu'] ?><?php } ?></textarea><br /> <?php if($mode_edition == 0) { ?> <input type="file" name="miniature" /><br /><br /> <?php } ?> <input type="submit" value="Envoyer l'article" /> <a href="index_admin.php">retour</a> </form> <br /> <?php if (isset($message)) { echo $message;} ?> </body> </html>

La partie articles.php

<?php $bdd = new PDO("mysql:host=localhost;dbname=flambeaux_site1;charset=utf8", "root", "root"); if(isset($_GET['id']) AND !empty($_GET['id'])) { $get_id = htmlspecialchars($_GET['id']); $article = $bdd->prepare('SELECT * FROM articles WHERE id = ?'); $article->execute(array($get_id)); if($article->rowCount() == 1) { $article = $article->fetch(); $titre = $article['titre']; $contenu = $article['contenu']; } else { die('Cet article n\'existe pas !'); } } else { die('Erreur'); } ?> <!DOCTYPE html> <html> <head> <title>Article</title> <meta charset="utf-8"> </head> <body> <h1><?= $titre ?></h1> <p><?= $contenu ?></p> </body> </html>

Et enfin la partie index.php

<html> <head> <title>Site Flambeaux et CLaires Flammes</title> <meta charset="utf-8" /> <section id="one" class="wrapper style1"> <div class="inner"> </head> <body class="landing"> <article class=""> <div class="content"> <a href="redaction.php">Rediger un nouvelle article</a> </div> </article> <?php while ($a = $articles -> fetch()) { ?> <article class="feature right"> <span class="image"><img src="images/amougie.jpg" alt=""></span> <div class="content"> <a href="articles.php?id=<?= $a['id'] ?>"> <h2> <?= $a['titre'] ?> </h2> </a> <p> <?= $a['contenu'] ?></p><br/> <a href="redaction.php?edit=<?= $a['id'] ?>">Modifier </a> / <a href="supprimer.php?id=<?= $a['id'] ?>">Supprimer </a> </div> </article> <?php } ?> </div> </section> </body> </html>

J'obtien la phrase "array(0) { } bool(false)" alors que je ne devrais pas obtenir ca et mon image ne se deplace pas dans mon dossier creer a cette effet.

2 réponses

Lartak, il y a 9 ans

Bonsoir.
Tu as mal vu comment utiliser cette fonction, il te faut plutôt faire :

if (exif_imagetype($_FILES['miniature']['name']) == IMAGETYPE_JPEG) { // etc ... }
leocoinsin, il y a 9 ans

Ok merci eaucoup de ton aide je vais essayer