solution pour upload multiple
je suis sur un projets et je bloc depuis plusieurs jours sur ma solutions pour poster plusieurs images sur mon site web. je crois mon code source juste mais rien ne marge j'eesais meme de savoir si les photo on ete passé a la variable $_FILES aucun resultat. je vous donne mon code:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
<?php
echo $_FILES['photo']['name'][0];
echo $_FILES['photo']['name'][1];
echo $_FILES['photo']['name'][2];
echo $_FILES['photo']['name'][3];
if($_POST) {
/* variables à modifier */
$taillemax = 1048576; // taille max d'un fichier (multiple de 1024)
$rep = "upload/"; // répertoire de destination
$maxfichier = 4; // nombre maximal de fichiers
/* fin des modifications */
$msg = array();
// $photos = $_FILES['photo']; simplication du tableau $_FILES
for($i=0; $i < $maxfichier; $i++) {
$extensions_valides = array( 'jpg' , 'jpeg' , 'gif' , 'png' );
//1. strrchr renvoie l'extension avec le point (« . »).
//2. substr(chaine,1) ignore le premier caractère de chaine.
//3. strtolower met l'extension en minuscules.
$extension_upload = strtolower( substr( strrchr($_FILES['photo']['name'][$i], '.') ,1) );
if ( in_array($extension_upload,$extensions_valides) ) {
if ($taillemax < $_FILES['photo']['size'][$i] )
echo "Le fichier est trop Lourd";
else {
//Créer un dossier 'fichiers/1/'
mkdir($rep, 0777, true);
//Créer un identifiant difficile à deviner
$nom = md5(uniqid(rand(), true));
$nom = "{$rep}/{$nom}.{$extension_upload}";
$resultat = move_uploaded_file($_FILES['photo']['tmp_name'][$i],$nom);
if ($resultat) echo "Transfert réussi";
else echo "Echec transfert";
}
}
else echo "Veuillez choisir une image";
}
}
?> |
code html
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
<form action="upload.php" method="post" enctype="multipart/form-data">
<table class="boduret" width="681" border="0" cellspacing="5">
<tr><td><span class="post">Photo 1:</span></td>
<td colspan="3"><p class="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1048576">
<input type="file" name="photo[]" id="photo" />
</p><span class="error"><? echo $msg[$i]; ?></span></td>
</tr>
<tr>
<td><span class="post">Photo 2:</span></td>
<td colspan="3"><p class="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1048576">
<input type="file" name="photo[]" id="photo" />
</p><span class="error"><? echo $msg[$i]; ?></span></td>
</tr>
<tr>
<td><span class="post">Photo 3:</span></td>
<td colspan="3"><p class="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1048576">
<input type="file" name="photo[]" id="photo" />
</p><span class="error"><? echo $msg[$i]; ?></span></td>
</tr>
<tr>
<td><span class="post">Photo 4:</span></td>
<td colspan="3"><p class="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1048576">
<input type="file" name="photo[]" id="photo" />
</p><span class="error"><? echo $msg[$i]; ?></span></td>
</tr>
<tr >
<td colspan="2" align="right"><input type="reset" name="submit" value="Réinitialiser" /></td>
<td width="362" colspan="2" align="center"><input type="submit" name="submit" value="Envoyer" /></td>
</tr>
</table>
</form> |