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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
<?
if($_POST) {
$msg = array(); // message
$fichier = $_FILES['lefichier']; // simplication du tableau $_FILES
for($i=0; $i<count($fichier['name']); $i++) {
// nom du fichier original = nom par défaut
$nom = $fichier['name'][$i];
// test existence fichier
if(!strlen($nom)) {
$msg[] = "Aucun fichier !";
continue;
}
// si un nouveau nom est renseigné (avec extension correcte)
if(eregi($nametype, $_POST['lenom'][$i]))
$nom = $_POST['lenom'][$i];
// répertoire de destination
$destination = $rep.$nom;
// test erreur (PHP 4.3)
if($fichier['error'][$i]) {
switch($fichier['error'][$i]) {
// dépassement de upload_max_filesize dans php.ini
case UPLOAD_ERR_INI_SIZE:
$msg[] = "Fichier trop volumineux !"; break;
// dépassement de MAX_FILE_SIZE dans le formulaire
case UPLOAD_ERR_FORM_SIZE:
$msg[] = "Fichier trop volumineux (supérieur à ".(INT)($taillemax/1024)." Mo)"; break;
// autres erreurs
default:
$msg[] = "Impossible d'uploader le fichier !";
}
}
// test taille fichier
elseif($fichier['size'][$i] > $taillemax)
$msg[] = "Fichier $nom trop volumineux : ".$fichier['size'][$i];
// test type fichier
elseif(!eregi($filetype, $fichier['type'][$i]))
$msg[] = "Fichier $nom de type incorrect : ".$fichier['type'][$i];
// test upload sur serveur (rep. temporaire)
elseif(!@is_uploaded_file($fichier['tmp_name'][$i]))
$msg[] = "Impossible d'uploader $nom";
// test transfert du serveur au répertoire
elseif(!@move_uploaded_file($fichier['tmp_name'][$i], $destination))
$msg[] = "Problème de transfert avec $nom";
else
$msg[] = "Fichier <b>$nom</b> téléchargé avec succès !";
}
// affichage confirmation
for($i=0; $i<=count($msg); $i++)
echo "<p>$msg[$i]</p>";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Upload</title>
<meta name="author" content="Pierre Pesty">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?
/* variables à modifier */
$taillemax = 1512000000; // taille max d'un fichier (multiple de 1024)
$filetype = "(jpeg|gif|jpg|png)"; // types de fichiers acceptés, séparés par |
$nametype = "(.jpeg||.gif|.jpg|.jpeg)"; // extensions correspondantes
$rep = "upload/"; // répertoire de destination
$maxfichier = 10; // nombre maximal de fichiers
/* fin des modifications */
$recursif = "http://127.0.0.1/site/index.php?dest=2"; // simplification du fichier courant
### insérer le traitement ci-après ###
if(!$upload = $_GET['upload']) $upload = $_POST['upload'];
if(!$upload || $upload > $maxfichier) $upload = 1; // protection
// choix du nombre $upload de fichier(s)
echo "<form action='$recursif' method='post'>\n";
echo "Quantité <select name='upload' onChange=\"window.open(this.options[this.selectedIndex].value,'_self')\">\n";
for($i=1; $i<=$maxfichier; $i++) {
echo "<option value='$recursif&upload=$i'";
if($i == $upload) echo " selected";
echo ">$i\n";
}
echo "</select>\n";
echo "<input name='upload' value='$upload' size='3'>\n";
echo "<input type='submit' value='Modifier'></form>\n";
// le formulaire
echo "<form action='$recursif' enctype='multipart/form-data' method='post'>\n";
// boucle selon nombre de fichiers $upload
for($i=1; $i<=$upload; $i++) {
echo "<p>Nom $i <input name='lenom[]'>\n";
echo "<input type='hidden' name='MAX_FILE_SIZE' value='$taillemax'>";
echo "Fichier <input type='file' name='lefichier[]'></p>\n";
}
?>
<input type='submit' value='Envoyer'>
</form>
</body>
</html> |
Partager