A voir également:
- Telecharger
- Télécharger clavier arabe - Télécharger - Divers Web & Internet
- Telecharger word 2013 - Télécharger - Traitement de texte
- Telecharger ccleaner gratuit - Télécharger - Nettoyage
- Télécharger musique gratuitement sur pc - Télécharger - Conversion & Extraction
- Telecharger zuma - Télécharger - Jeux vidéo
1 réponse
cette fonction est simple
Je te donne un script complet d'upload:
<?php
$max = 10*1024*1024; //Taille Maxi
if(empty($_POST['posted']))
{
?>
Envoyez une image (max <?php echo $max/(1024*1024); ?> MB):<br />
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max; ?>">
<input type="file" name="avatar"><br />
<input type="submit" name="posted" value="Envoyer le fichier">
</form>
<?php
}
elseif(isset($_POST['posted']))
{
$dossier = "./uploads/"; //Dossier d'upload
$fichier = basename($_FILES['avatar']['type']); // Récupération de l'extension, jpg, png,...
$fichier2 = '.'.$fichier.''; // Ajout d'un point à l'extension
$taille_maxi = $max; //Définition de la taille max
$taille = filesize($_FILES['avatar']['tmp_name']); // Récupération de la taille de l'image
$extensions = array('.png', '.gif', '.jpg', '.jpeg', '.PNG', '.GIF', '.JPG', '.JPEG'); //Array des extensions permises
$extension = strrchr($_FILES['avatar']['name'], '.'); // Récupération du nom du fichier abc.png
//Début des vérifications de sécurité...
if(!in_array($extension, $extensions)) //Si l'extension n'est pas dans le tableau
{
$erreur = 'Vous devez uploader un fichier de type png, gif, jpg, ou jpeg...';
}
if($taille>$taille_maxi) // Si c'est trop gros
{
$erreur = 'Le fichier est trop gros, il doit faire moins de '.$max/(1024*1024).' MB ('.$max.' Octects)...';
}
if(!isset($erreur)) //S'il n'y a pas d'erreur, on upload
{
//On formate le nom du fichier ici...
$caracteres = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "z", "c", "o", "p", "f", "x", "e", "q", "g", "k", "i", "j", "h", "j", "h", "l", "m", "l", "m", "n", "o", "p", "f", "r", "s", "t", "u", "v", "w", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "d", "y", "c", "a", "z", "c", "x", "e", "q", "g", "k", "i", "j", "h", "l", "m", "n", "o", "p", "f", "r", "s", "t", "u", "j", "h", "l", "m", "v", "w", "d", "y", "c", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "z", "c", "o", "p", "f", "x", "e", "q", "g", "k", "i", "j", "h", "j", "h", "l", "m", "l", "m", "n", "o", "p", "f", "r", "s", "t", "u", "v", "w", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "d", "y", "c", "a", "z", "c", "x", "e", "q", "g", "k", "i", "j", "h", "l", "m", "n", "o", "p", "f", "r", "s", "t", "u", "j", "h", "l", "m", "v", "w", "d", "y", "c", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "z", "c", "o", "p", "f", "x", "e", "q", "g", "k", "i", "j", "h", "j", "h", "l", "m", "l", "m", "n", "o", "p", "f", "r", "s", "t", "u", "v", "w", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "d", "y", "c", "a", "z", "c", "x", "e", "q", "g", "k", "i", "j", "h", "l", "m", "n", "o", "p", "f", "r", "s", "t", "u", "j", "h", "l", "m", "v", "w", "d", "y", "c", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$caracteres_aleatoires = array_rand($caracteres, 40);
$nom_changer = "";
foreach($caracteres_aleatoires as $i)
{
$nom_changer .= $caracteres[$i];
}
if(move_uploaded_file($_FILES['avatar']['tmp_name'], $dossier . $nom_changer . $fichier2)) //Copie vers le fichier upload // Si la fonction renvoie TRUE, c'est que ça a fonctionné...
{
echo 'Upload réussi';
}
}
else //Sinon (la fonction renvoie FALSE).
{
echo 'Échec de l\'upload !';
}
}
Je te donne un script complet d'upload:
<?php
$max = 10*1024*1024; //Taille Maxi
if(empty($_POST['posted']))
{
?>
Envoyez une image (max <?php echo $max/(1024*1024); ?> MB):<br />
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max; ?>">
<input type="file" name="avatar"><br />
<input type="submit" name="posted" value="Envoyer le fichier">
</form>
<?php
}
elseif(isset($_POST['posted']))
{
$dossier = "./uploads/"; //Dossier d'upload
$fichier = basename($_FILES['avatar']['type']); // Récupération de l'extension, jpg, png,...
$fichier2 = '.'.$fichier.''; // Ajout d'un point à l'extension
$taille_maxi = $max; //Définition de la taille max
$taille = filesize($_FILES['avatar']['tmp_name']); // Récupération de la taille de l'image
$extensions = array('.png', '.gif', '.jpg', '.jpeg', '.PNG', '.GIF', '.JPG', '.JPEG'); //Array des extensions permises
$extension = strrchr($_FILES['avatar']['name'], '.'); // Récupération du nom du fichier abc.png
//Début des vérifications de sécurité...
if(!in_array($extension, $extensions)) //Si l'extension n'est pas dans le tableau
{
$erreur = 'Vous devez uploader un fichier de type png, gif, jpg, ou jpeg...';
}
if($taille>$taille_maxi) // Si c'est trop gros
{
$erreur = 'Le fichier est trop gros, il doit faire moins de '.$max/(1024*1024).' MB ('.$max.' Octects)...';
}
if(!isset($erreur)) //S'il n'y a pas d'erreur, on upload
{
//On formate le nom du fichier ici...
$caracteres = array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "z", "c", "o", "p", "f", "x", "e", "q", "g", "k", "i", "j", "h", "j", "h", "l", "m", "l", "m", "n", "o", "p", "f", "r", "s", "t", "u", "v", "w", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "d", "y", "c", "a", "z", "c", "x", "e", "q", "g", "k", "i", "j", "h", "l", "m", "n", "o", "p", "f", "r", "s", "t", "u", "j", "h", "l", "m", "v", "w", "d", "y", "c", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "z", "c", "o", "p", "f", "x", "e", "q", "g", "k", "i", "j", "h", "j", "h", "l", "m", "l", "m", "n", "o", "p", "f", "r", "s", "t", "u", "v", "w", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "d", "y", "c", "a", "z", "c", "x", "e", "q", "g", "k", "i", "j", "h", "l", "m", "n", "o", "p", "f", "r", "s", "t", "u", "j", "h", "l", "m", "v", "w", "d", "y", "c", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "a", "z", "c", "o", "p", "f", "x", "e", "q", "g", "k", "i", "j", "h", "j", "h", "l", "m", "l", "m", "n", "o", "p", "f", "r", "s", "t", "u", "v", "w", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, "d", "y", "c", "a", "z", "c", "x", "e", "q", "g", "k", "i", "j", "h", "l", "m", "n", "o", "p", "f", "r", "s", "t", "u", "j", "h", "l", "m", "v", "w", "d", "y", "c", 0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
$caracteres_aleatoires = array_rand($caracteres, 40);
$nom_changer = "";
foreach($caracteres_aleatoires as $i)
{
$nom_changer .= $caracteres[$i];
}
if(move_uploaded_file($_FILES['avatar']['tmp_name'], $dossier . $nom_changer . $fichier2)) //Copie vers le fichier upload // Si la fonction renvoie TRUE, c'est que ça a fonctionné...
{
echo 'Upload réussi';
}
}
else //Sinon (la fonction renvoie FALSE).
{
echo 'Échec de l\'upload !';
}
}