After correcting a typo and succesfully sent to my database the mostly text fields to my sql database, i was trying now to upload the two files the user sends in the form (2 faces of an ID card) to my server. However, despite having the encpart="multipart/form-data"
and the post method in the form's tag, and having checked the names of both the input fields and their uses in the php files, php doesn't find the files sent.
I have files upload activated in my php.ini, with upload limits well above the size of the test images i sent (respectively 261 and 193 KB), with max file upload at 8 MB and post method limits at 250 MB. I am using php 8.1.0.
The html form (all of the other fields except the file inputs works) :
<section id="form">
<form action="submit_signature.php" method="post" encpart="multipart/form-data">
<label for="nom">Nom :</label><br>
<input type=text name="Nom" id="Nom" class="big" required><br>
<label for="prenom">Prénom :</label><br>
<input type=text name="Prénom" id="prenom" class="big" required><br>
<label for="pseudo">Pseudonyme :</label><br>
<input type=text name="Pseudonyme" id="pseudo" class="big" placeholder="Nom sous lequel vous apparaîtrez dans les dernières signatures."><br>
<label for="daten">Date de naissance :</label><br>
<input type=date name="Datenaissance" id="daten" min="1907-03-04" max="2006-01-01" class="smol" required><br>
<label for="email">E-mail :</label><br>
<input type=email name="Email" id="email" class="smol" placeholder="Adresse mail en cas de pépin pour vous contacter !" required><br>
<label for="tel">N° de téléphone :</label><br>
<input type=tel name="tel" id="tel" placeholder="+(X)XX X XX XX XX XX" class="smol" pattern="+[0-972]{3}-[0-9]{1}-[0-9]{2}-[0-9]{2}-[0-9]{2}-[0-9]{2}" required><br>
<label for="idavant">Face avant de votre pièce d'identité :</label>
<input type="file" id="idavant" name="idavant" value="download" required><br>
<label for="idarriere">Face arrière de votre pièce d'identité :</label>
<input type="file" id="idarriere" name="idarriere" value="download" required><br>
<label for="motiv">Motivation :</label><br>
<textarea name="Motivation" id="motiv" class="big" placeholder="Dites nous pourquoi vous signez cette pétition, si cela vous dit !"></textarea><br><br>
<input type=submit name="Envoyer" id="envoyer" value="Envoyer">
<input type=reset name="Réinitialiser" id="reset" value="Réinitialiser">
</form>
<br>
<p id="formwarning">NB : Votre signature ne sera pas prise en compte tant que votre identité ne sera pas vérifiée, et votre inscription certifiée par nos administrateurs™.</p>
</section>
The php code i use to check and then send the file to the server. I've only done tests on the idavant
file.
// Tests if the file exists and doesn't have errors
if (isset($_FILES['idavant']['tmp_name']) && $_FILES['idavant']['error'] == 0) {
// Tests if the file isn't too big
if ($_FILES['idavant']['size'] <= 15000000)
{
// Tests if it is an authorized file extension
$fileInfo = pathinfo($_FILES['idavant']['name']);
$extension = $fileInfo['extension'];
$allowedExtensions = ['jpg', 'jpeg', 'gif', 'png'];
if (in_array($extension, $allowedExtensions))
{
// If all conditions are satisfied, file is uploaded definitively to the server
move_uploaded_file($_FILES['idavant']['tmp_name'], 'uploads/' . basename($_FILES['idavant']['name']));
echo "L'envoi a bien été effectué !";
} else {
error_log("Erreur de téléchargement du fichier\n", 3, "log1.txt");
}
} else {
error_log("Erreur de téléchargement du fichier : Le fichier est trop gros;\n", 3, "log1.txt");
}
} else {
error_log("Erreur de téléchargement du fichier : Erreur dans la procédure de transfert entre les pages;\n", 3, "log1.txt");
//Tests if the file exists. Always throws the file doesn't exist error.
if (isset($_FILES['idavant']['tmp_name'])) {
echo "Le fichier existe.";
} else {
echo "Le fichier n'existe pas";
}
//Tests if the file hasn't errors attached to it. This time, it tells me that there is no errors.
if ($_FILES['idavant']['error'] == 0) {
echo "Le fichier est bien sans erreur";
} else {
echo "bah non mdr";
}
//Dumping the content of the different $_FILES variable. Returns an empty array and two NULLS.
echo "<br/>".var_dump($_FILES);
echo "<br/>".var_dump($_FILES['idavant']);
echo "<br/>".var_dump($_FILES['idarriere']);
}
I've corrected the typos in my code pointed out by the comments + another one that a friend of me showed (used the wrong upload folder). After that and a few test my code works now, so i'll leave the corrected one there :
if (isset($_FILES['idarriere']['tmp_name']) && $_FILES['idarriere']['error'] == 0) {
// Tests if the file isn't too big
if ($_FILES['idarriere']['size'] <= 15000000)
{
// Tests if it is an authorized file extension
$fileInfo = pathinfo($_FILES['idarriere']['name']);
$extension = $fileInfo['extension'];
$allowedExtensions = ['jpg', 'jpeg', 'gif', 'png'];
if (in_array($extension, $allowedExtensions))
{
// If all conditions are satisfied, file is uploaded definitively to the server
move_uploaded_file($_FILES['idarriere']['tmp_name'], 'fichiers/' . basename($_FILES['idarriere']['name']));
$target_file2 = 'fichiers/'.basename($_FILES['idarriere']['name']);
echo " L'envoi de la face verso a bien été effectué !";
} else {
error_log("Erreur de téléchargement du fichier\n", 3, "log1.txt");
}
} else {
error_log("Erreur de téléchargement du fichier : Le fichier est trop gros;\n", 3, "log1.txt");
}
} else {
error_log("Erreur de téléchargement du fichier : Erreur dans la procédure de transfert entre les pages;\n", 3, "log1.txt");
//Tests if the file exists.
if (isset($_FILES['idarriere']['tmp_name'])) {
echo " Le fichier existe.";
} else {
echo " Le fichier n'existe pas";
}
//Tests if the file hasn't errors attached to it.
if ($_FILES['idarriere']['error'] == 0) {
echo " Le fichier est bien sans erreur";
} else {
echo " Il semble y avoir une erreur dans le fichier.";
}
}```