Upload form + modif en php

Résolu/Fermé
griko Messages postés 343 Date d'inscription samedi 31 janvier 2009 Statut Membre Dernière intervention 29 avril 2014 - 28 août 2009 à 00:15
griko Messages postés 343 Date d'inscription samedi 31 janvier 2009 Statut Membre Dernière intervention 29 avril 2014 - 1 sept. 2009 à 23:04
Bonjour,
Comment faire pour uploader une image en changeant sont nom ?

Merci d'avance.

6 réponses

griko Messages postés 343 Date d'inscription samedi 31 janvier 2009 Statut Membre Dernière intervention 29 avril 2014 10
1 sept. 2009 à 23:04
Voici le code :
if (file_exists("../../../film/images/".$idfilm."/"))
{
	ftp_mkdir("../../../film/images/".$idfilm."/");
}
$dossier = "../../../film/images/".$idfilm."/"; //Dossier d'upload
    $fichier = basename($_FILES['fichierphoto']['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['fichierphoto']['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['fichierphoto']['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..., veuillez <a href="ajfichefilms4.php?fiche='.$idfilm.'&amp;nbrphoto='.$nbrphoto.'&amp;numtof='.$numtof.'">recommencer</a>';
    }
    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)..., veuillez <a href="ajfichefilms4.php?fiche='.$idfilm.'&amp;nbrphoto='.$nbrphoto.'&amp;numtof='.$numtof.'">recommencer</a>';
    }
    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];
			 }
			 $nom_changer = $idfilm.$numtof;
		if(move_uploaded_file($_FILES['fichierphoto']['tmp_name'], $dossier . $nom_changer . $fichier2)) //Copie vers le fichier upload // Si la fonction renvoie TRUE, c'est que ça a fonctionné...
        {
            $sql = "INSERT INTO galphotosfilm (nomfilm_galphotosfilm, titre_galphotosfilm, libelle_galphotosfilm, src_galphotosfilm, dateaj_galphotosfilm, auteur_galphotosfilm) VALUES ('".$idfilm."', '".$titre_galphotosfilm1."', '".$libelle_galphotosfilm1."', '".$nom_changer.$fichier2."', '".date('Y-m-d')."', '".$_SESSION["login"]."');";
			$req2 = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error()) ; //exécution de la requête SQL
			echo 'Upload r&eacute;ussi';
		}
    }
	else //Sinon (la fonction renvoie FALSE).
	{
		echo '&Eacute;chec de l\'upload !';
	}
1
Voici un script PHP, il upload et change le nom du fichier par un clé généré je l'utilise sur mon site web... je les écrits avec plusieurs bouts de codes mais ça fonctionne :D
<?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&eacute;ussi';
		}
    }
	else //Sinon (la fonction renvoie FALSE).
	{
		echo '&Eacute;chec de l\'upload !';
	}
}
?>
0
griko Messages postés 343 Date d'inscription samedi 31 janvier 2009 Statut Membre Dernière intervention 29 avril 2014 10
28 août 2009 à 08:17
Sa na pas l'air très propre ?! si ?
Et il n'y a pas plus simple ?
0
avion-f16 Messages postés 19246 Date d'inscription dimanche 17 février 2008 Statut Contributeur Dernière intervention 21 avril 2024 4 497
28 août 2009 à 11:09
Tu peux génèrer le nom avec time() et microtime() :
$nom = srand((double)microtime()*1000000);
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
Le fait que ça n'as pas l'air propre, c'est que comment ça marche mets une limite width par tres large et ça fait automatique un retour de ligne... compie le code... et essaie le tu vas voir qu'il fonctionne !!!
0
griko Messages postés 343 Date d'inscription samedi 31 janvier 2009 Statut Membre Dernière intervention 29 avril 2014 10
1 sept. 2009 à 22:56
Bonsoir,
j'ai testé le code mais il ne veut pas uploader !!

Pourquoi?
0