bug dans la gestion des exceptions dans un formulaire

yvesman131
Invité n'ayant pas de compte PHPfrance

24 janv. 2022, 19:22

bonjour

voici mon problème

j'ai un formulaire html classique qui fonctionne MAIS !!

je voudrais créer des exceptions au cas où il n'y aurait pas beaucoup d'images à insérer

pour cela j'ai mis un if(isset($_FILES) autour du code

avec cela, le code est correctement inséré mais j'ai une erreur si le fichier reste vide : Erreur fatale : Erreur de valeur non détectée : Le chemin ne peut pas être vide dans /....

donc je remplace $_FILES par $_POST mais dans ce cas aucun fichier n'est mis à jour (présent ou non)

donc j'ai mis un var_dump($_POST) pour voir ce qui se passe et surprendre !! aucun $_POST['photo'] et aucun $_POST['alt'] n'apparaissent !!

question :

comment puis-je faire pour traiter une exception au cas où il n'y aurait qu'une seule photo à enregistrer sans obtenir d'erreur de copie ou d'erreur bdr ?

voici les codes :
<?php
require_once("init.inc.php");
//--- ENREGISTREMENT PRODUIT ---//
if(!empty($_POST))
var_dump($_POST);
{
if ( isset($_FILES['photo1']))
{
$photo_bdd = "";
$photo_dossier = "";
$v=1;
$prepa_name1 = substr($_POST['titre'], 0, 45);
$name1= $prepa_name1 . $v . ".jpg" ;
$photo_bdd1 = RACINE_SITE . "photos/$_POST[categorie]/$name1";
$photo_dossier = $_SERVER['DOCUMENT_ROOT'] . RACINE_SITE . "/photos/$_POST[categorie]/$name1";
copy($_FILES['photo1']['tmp_name'],$photo_dossier);
$_FILES['photo1'] ="";
$v++ ;
}
$character= "'";
$replace = "\'" ;
$description1 = $_POST['description1'];
$description1 = str_replace ( $character , $replace , $description1, ) ;
//$character= "jusqu'a";
//$replace = "jusqu\'a" ;
//$description1 = $_POST['description1'];
//$description1 = str_replace ( $character , $replace , $description1, ) ;
echo $description1 ;

if ( isset($_FILES['photo2']))
{
$photo_bdd = "";
$photo_dossier = "";
$prepa_name2 = substr($_POST['titre'], 0, 45);
$name2= $prepa_name2 . $v . ".jpg" ;
$photo_bdd2 = RACINE_SITE . "photos/$_POST[categorie]/$name2";
$photo_dossier = $_SERVER['DOCUMENT_ROOT'] . RACINE_SITE . "/photos/$_POST[categorie]/$name2";
copy($_FILES['photo2']['tmp_name'],$photo_dossier);
$v++ ;
}
$character= "'";
$replace = "\'" ;
$description2 = $_POST['description2'];
$description2 = str_ireplace ( $character , $replace , $description2 ) ;
//echo $description2 ;

if ( isset($_FILES['photo3']))
{
$photo_bdd = "";
$photo_dossier = "";
$prepa_name3 = substr($_POST['titre'], 0, 45);
$name3= $prepa_name3 . $v . ".jpg" ;
$photo_bdd3 = RACINE_SITE . "photos/$_POST[categorie]/$name3";
$photo_dossier = $_SERVER['DOCUMENT_ROOT'] . RACINE_SITE . "/photos/$_POST[categorie]/$name3";
copy($_FILES['photo3']['tmp_name'],$photo_dossier);
$v++ ;
}
$character= "'";
$replace = "\'" ;
$description3 = $_POST['description3'];
$description3 = str_ireplace ( $character , $replace , $description3, ) ;
//echo $description3 ;

if ( isset($_FILES['photo4']))
{
$photo_bdd = "";
$photo_dossier = "";
$prepa_name4 = substr($_POST['titre'], 0, 45);
$name4= $prepa_name4 . $v . ".jpg" ;
$photo_bdd4 = RACINE_SITE . "photos/$_POST[categorie]/$name4";
$photo_dossier = $_SERVER['DOCUMENT_ROOT'] . RACINE_SITE . "/photos/$_POST[categorie]/$name4";
copy($_FILES['photo4']['tmp_name'],$photo_dossier);
$v++ ;
}
$character= "'";
$replace = "\'" ;
$description4 = $_POST['description4'];
$description4 = str_ireplace ( $character , $replace , $description4, ) ;
//echo $description4 ;

if ( isset($_FILES['photo5']))
{
$photo_bdd = "";
$photo_dossier = "";
$prepa_name5 = substr($_POST['titre'], 0, 45);
$name5= $prepa_name5 . $v. ".jpg" ;
$photo_bdd5 = RACINE_SITE . "photos/$_POST[categorie]/$name5";
$photo_dossier = $_SERVER['DOCUMENT_ROOT'] . RACINE_SITE . "/photos/$_POST[categorie]/$name5";
copy($_FILES['photo5']['tmp_name'],$photo_dossier);
$v++ ;
}

$character= "'" ;
$replace = "\'" ;
$description5 = $_POST['description5'];
$description5 = str_ireplace ( $character , $replace , $description5, ) ;
//echo $description5 ;

$character= "'" ;
$replace = "\'" ;
$description6 = $_POST['description6'];
$description6 = str_replace ( $character , $replace , $description6, ) ;
//echo $description6 ;


$character= "'";
$replace = "\'" ;
$detail1 = $_POST['detail1'];
$detail1 = str_ireplace ( $character , $replace , $detail1, ) ;

$character= "'";
$replace = "\'" ;
$detail2 = $_POST['detail2'];
$detail2 = str_ireplace ( $character , $replace , $detail2, ) ;

$character= "'";
$replace = "\'" ;
$detail3 = $_POST['detail3'];
$detail3 = str_ireplace ( $character , $replace , $detail3, ) ;

$character= "'";
$replace = "\'" ;
$detail4 = $_POST['detail4'];
$detail4 = str_ireplace ( $character , $replace , $detail4, ) ;

$character= "'";
$replace = "\'" ;
$detail5 = $_POST['detail5'];
$detail5 = str_ireplace ( $character , $replace , $detail5, ) ;

$character= "'";
$replace = "\'" ;
$detail6 = $_POST['detail6'];
$detail6 = str_ireplace ( $character , $replace , $detail6, ) ;

$character= "'";
$replace = "\'" ;
$detail7 = $_POST['detail7'];
$detail7 = str_ireplace ( $character , $replace , $detail7, ) ;


$character= "'";
$replace = "\'" ;
$detail_livraison = $_POST['detail_livraison'];
$detail_livraison = str_ireplace ( $character , $replace , $detail_livraison, ) ;

$character= "'";
$replace = "\'" ;
$detail_prix = $_POST['detail_prix'];
$detail_prix = str_ireplace ( $character , $replace , $detail_prix, ) ;

echo ' <br/>';
$exec =executeRequete("INSERT INTO produit
(
groupe,
categorie,
titre,
description1,
evaluation1,
evaluation2,
detail_livraison,
detail1,
detail2,
detail3,
detail4,
detail5,
detail6,
detail7,
stock,
prix,
detail_prix,
photo1,
alt1,
photo2,
alt2,
photo3,
alt3,
photo4,
alt4,
photo5,
alt5,
description2,
description3,
description4,
description5,
description6,
lien,
magasin,
divers
)
values
(
'$_POST[groupe]',
'$_POST[categorie]',
'$_POST[titre]',
'$description1',
'$_POST[evaluation1]',
'$_POST[evaluation2]',
'$detail_livraison',
'$detail1',
'$detail2',
'$detail3',
'$detail4',
'$detail5',
'$detail6',
'$detail7',
'$_POST[stock]',
'$_POST[prix]',
'$detail_prix',
'$photo_bdd1',
'$prepa_name1',
'$photo_bdd2',
'$prepa_name2',
'$photo_bdd3',
'$prepa_name3',
'$photo_bdd4',
'$prepa_name4',
'$photo_bdd5',
'$prepa_name5',
'$description2',
'$description3',
'$description4',
'$description5',
'$description6',
'$_POST[lien]',
'$_POST[magasin]',
'$_POST[divers]'
)");

$contenu .= '<div class="validation">Le produit a été ajouté</div>';
}
?>
<?php
if(isset($_GET['action']) && ($_GET['action'] == 'ajout'))
{
echo '
<h1> Formulaire Produits </h1>
<form method="post" enctype="multipart/form-data" action="">

<label for="groupe">groupe</label><br>
<input type="text" id="groupe" name="groupe" placeholder="Groupe"><br><br>

<label for="categorie">categorie</label><br>
<input type="text" id="categorie" name="categorie" placeholder="Categorie"><br><br>

<label for="titre">titre</label><br>
<input type="text" id="titre" name="titre" placeholder="Titre"><br><br>

<label for="description1">description1</label><br>
<input type="text" id="description1" name="description1" placeholder="Description1"><br><br>

<label for="evaluation1">evaluation1</label><br>
<input type="text" id="evaluation1" name="evaluation1" placeholder="Evaluation1"><br><br>

<label for="evaluation2">evaluation2</label><br>
<input type="text" id="evaluation2" name="evaluation2" placeholder="Evaluation2"><br><br>

<label for="detail_livraison">Detail_livraison</label><br>
<input type="text" id="detail_livraison" name="detail_livraison" placeholder="Detail_livraison"><br><br>

<label for="detail1">detail1</label><br>
<input type="text" id="detail1" name="detail1" placeholder="detail1"><br><br>

<label for="detail2">detail2</label><br>
<input type="text" id="detail2" name="detail2" placeholder="detail2"><br><br>

<label for="detail3">detail3</label><br>
<input type="text" id="detail3" name="detail3" placeholder="detail3"><br><br>


<label for="detail4">detail4</label><br>
<input type="text" id="detail4" name="detail4" placeholder="detail4"><br><br>


<label for="detail5">detail5</label><br>
<input type="text" id="detail5" name="detail5" placeholder="detail5"><br><br>


<label for="detail6">detail6</label><br>
<input type="text" id="detail6" name="detail6" placeholder="detail6"><br><br>


<label for="detail7">detail7</label><br>
<input type="text" id="detail7" name="detail7" placeholder="detail7"><br><br>

<label for="stock">stock</label><br>
<input type="text" id="stock" name="stock" placeholder="stock"><br><br>

<label for="prix">prix</label><br>
<input type="text" id="prix" name="prix" placeholder="prix"><br><br>

<label for="detail_prix">detail_prix</label><br>
<input type="text" id="detail_prix" name="detail_prix" placeholder="Detail_prix "><br><br>

<label for="photo1">photo1</label><br>
<input type="file" id="photo1" name="photo1"><br><br>

<label for="photo2">photo2</label><br>
<input type="file" id="photo2" name="photo2"><br><br>

<label for="photo3">photo3</label><br>
<input type="file" id="photo3" name="photo3"><br><br>

<label for="photo4">photo4</label><br>
<input type="file" id="photo4" name="photo4"><br><br>

<label for="photo5">photo5</label><br>
<input type="file" id="photo5" name="photo5"><br><br>

<label for="description2">description2</label><br>
<input type="text" id="description2" name="description2" placeholder="Description2"><br><br>

<label for="description3">description3</label><br>
<input type="text" id="description3" name="description3" placeholder="Description3"><br><br>

<label for="description4">description4</label><br>
<input type="text" id="description4" name="description4" placeholder="Description4"><br><br>

<label for="description5">description5</label><br>
<input type="text" id="description5" name="description5" placeholder="Description5"><br><br>

<label for="description6">description6</label><br>
<input type="text" id="description6" name="description6" placeholder="Description6"><br><br>

<label for="lien">lien</label><br>
<input type="text" id="lien" name="lien" placeholder="Lien du produit"><br><br>

<label for="magasin">magasin</label><br>
<input type="text" id="magasin" name="magasin" placeholder="Amazone"><br><br>

<label for="divers">divers</label><br>
<input type="text" id="divers" name="divers" placeholder="divers"><br><br>

<input type="submit" value="enregistrement du produit">
</form>';
}
?>
voici le retour du var_dump($_POST)
array(25) { ["groupe"]=> string(6) "mlkmlk" ["categorie"]=> string(4) "test" ["titre"]=> string(4) "test" ["description1 "]=> string(0) "" ["evaluation1"]=> string(0) "" ["evaluation2"]=> string(0) "" ["detail_livraison"]=> string(0) "" [ "detail1"]=> chaîne(0) "" ["detail2"]=> chaîne(0) "" ["detail3"]=> chaîne(0) "" ["detail4"]=> chaîne(0) " " ["detail5"]=> chaîne(0) "" ["detail6"]=> chaîne(0) "" ["detail7"]=> chaîne(0) "" ["stock"]=> chaîne(0) ) "" ["prix"]=> string(0) "" ["detail_prix"]=> string(0) "" ["description2"]=> string(0) "" ["description3"]=> string(0) "" ["description4"]=> string(0) "" ["description5"]=> string(0) "" ["description6"]=> string(0) "" ["lien"]=> string(0) "" ["magasin"]=> string(0) "" ["divers"]=> chaîne(0) "" }chaîne(0) "" ["divers"]=> chaîne(0) "" }chaîne(0) "" ["divers"]=> chaîne(0) "" }
vous ne pouvez voir aucune photo et alt apparaît

merci pour votre aide

Petit nouveau ! | 2 Messages

26 janv. 2022, 01:28

Bonsoir,

Pour tester si un fichier est présent dans un champ, il faut tester sur $_FILES et non $_POST.

Quelque chose qui devrait ressembler à
isset($_FILES['nom_du_champ']) && $_FILES['nom_du_champ']['error'] === UPLOAD_ERR_OK;
Par ailleurs il y a quelques problématiques de sécurité à prendre en compte :

* il faut restreindre le type de fichier pouvant être uploadé à certains types souhaités (exemple, JPG, PNG, etc), afin d'éviter l'upload de fichiers non désirés (virus, etc)

* tel que présenté, c'est l'utilisateur qui décide dans quel dossier le fichier sera stocké ($_POST['categorie']) ; si le dossier n'existe pas, il n'est pas créé (pas de mkdir() présent) et cela reste une mauvaise idée, sauf à créer le dossier proprement en s'assurant que le nom du dossier ne contient rien d'invalide et reste bien dans photos/ (pas de tentative de se déplacer dans l'arborescence)

* Idem, le nom du fichier devrait être nettoyé pour supprimer tout caractère potentiellement problématique

* la fonction pour déplacer un fichier uploadé dans son dossier de destination est move_uploaded_file(), pas copy()

* il faudrait également sécuriser la requête SQL, je ne sais pas ce que fait la fonction executeRequete() mais en l'état une injection SQL devrait être assez simple à réaliser ; typiquement on ne devrait pas insérer directement une donnée $_POST ou $_GET brute sans aucun traitement (mysqli_real_escape_string(), filter_var(), ...)