Fonction File Uploads Php

Résolu/Fermé
aureliendu917 Messages postés 272 Date d'inscription mardi 27 novembre 2007 Statut Membre Dernière intervention 14 janvier 2011 - 25 juin 2009 à 22:18
aureliendu917 Messages postés 272 Date d'inscription mardi 27 novembre 2007 Statut Membre Dernière intervention 14 janvier 2011 - 26 juin 2009 à 00:47
Bonjour,
voila j'ai un probleme je voudrai que quand un personne uploads une images je voudrai que que le fichiers se renommer si elle existe deja est ce que vous pouvez m'aidez svp voici mon code

<?php
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
?>


Merci a vous
A voir également:

26 réponses

Dalida Messages postés 6728 Date d'inscription mardi 14 mai 2002 Statut Contributeur Dernière intervention 11 janvier 2016 920
25 juin 2009 à 22:38
fais afficher le nom généré pour le fichier et vérifies si il existe déjà sur ton disque :
<?php
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

while(file_exists($target_path))
	$target_path = $target_path.rand(0, 9);
	
var_dump($target_path);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
?>
1
Dalida Messages postés 6728 Date d'inscription mardi 14 mai 2002 Statut Contributeur Dernière intervention 11 janvier 2016 920
25 juin 2009 à 22:33
salut,

de tête, je penserais à :
<?php
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

while(file_exists($target_path))
	$target_path = $target_path.rand(0, 9);
	
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
?>
0
aureliendu917 Messages postés 272 Date d'inscription mardi 27 novembre 2007 Statut Membre Dernière intervention 14 janvier 2011 6
25 juin 2009 à 22:35
dsl sa marche pas !!!
0
aureliendu917 Messages postés 272 Date d'inscription mardi 27 novembre 2007 Statut Membre Dernière intervention 14 janvier 2011 6
25 juin 2009 à 22:42
merci sa marche mai le problème sait que le chiffre se mai après l'extension et sa donne ça images.png2
0
Dalida Messages postés 6728 Date d'inscription mardi 14 mai 2002 Statut Contributeur Dernière intervention 11 janvier 2016 920
25 juin 2009 à 22:49
ah ben oui, qu'est-ce que je suis c** !

désolé, je réfléchis pas j'applique du code bêtement…
<?php
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
while(file_exists($target_path))
{
	$name = pathinfo($target_path, 'filename');
	$ext = pathinfo($target_path, 'extension');
	$target_path = $name.rand(0, 9).$ext;
}
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']).
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
?>
0

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

Posez votre question
aureliendu917 Messages postés 272 Date d'inscription mardi 27 novembre 2007 Statut Membre Dernière intervention 14 janvier 2011 6
25 juin 2009 à 22:51
il y a encore un probleme lol :

Warning: pathinfo() expects parameter 2 to be long, string given in C:\wamp\www\ok.php on line 7

Warning: pathinfo() expects parameter 2 to be long, string given in C:\wamp\www\ok.php on line 8
The file bouton.png has been uploaded
0
Dalida Messages postés 6728 Date d'inscription mardi 14 mai 2002 Statut Contributeur Dernière intervention 11 janvier 2016 920
25 juin 2009 à 22:57
ce coup-ci je plaide non coupable !!!
j'ai pris les arguments dans le manuel PHP (version locale en CHM), "c'est pas d'ma faute m'sieur l'agent !".
-:oD

while(file_exists($target_path))
{
	$name = pathinfo($target_path, PATHINFO_FILENAME);
	$ext = pathinfo($target_path, PATHINFO_EXTENSION);
	$target_path = $name.rand(0, 9).$ext;
}
0
aureliendu917 Messages postés 272 Date d'inscription mardi 27 novembre 2007 Statut Membre Dernière intervention 14 janvier 2011 6
25 juin 2009 à 23:27
j'ai teste en ligne et sa m'affiche la même erreur !!
0
Dalida Messages postés 6728 Date d'inscription mardi 14 mai 2002 Statut Contributeur Dernière intervention 11 janvier 2016 920
25 juin 2009 à 23:40
grrrr !
le point bon sang !

bon ce coup -ci j'ai essayé, ça fonctionne !
while(file_exists($target_path))
{
	$name = pathinfo($target_path, PATHINFO_FILENAME);
	$ext = pathinfo($target_path, PATHINFO_EXTENSION);
	$target_path = $name.rand(0, 9).'.'.$ext;
	var_dump($target_path);
}
0
aureliendu917 Messages postés 272 Date d'inscription mardi 27 novembre 2007 Statut Membre Dernière intervention 14 janvier 2011 6
25 juin 2009 à 23:44
merci mais je sais je suis casse noisette mait il y a encore un probleme cet fois le fichier me vos pas dans le fichier "uploads"
0
Dalida Messages postés 6728 Date d'inscription mardi 14 mai 2002 Statut Contributeur Dernière intervention 11 janvier 2016 920
25 juin 2009 à 23:46
que t'affiche le "var_dump($target_path);" ?

ce n'est pas une adresse correcte ?

as-tu essayé sans le "var_dump($target_path);", ça peut être lui qui empêche la suite du code de s'exécuter correctement.
0
aureliendu917 Messages postés 272 Date d'inscription mardi 27 novembre 2007 Statut Membre Dernière intervention 14 janvier 2011 6
25 juin 2009 à 23:55
j'ai enlever le code :

var_dump($target_path); 


mait la sa copie plus du tous
0
Dalida Messages postés 6728 Date d'inscription mardi 14 mai 2002 Statut Contributeur Dernière intervention 11 janvier 2016 920
26 juin 2009 à 00:00
et quand il y avait le "var_dump($target_path);", il affichait quoi comme erreur ?
0
aureliendu917 Messages postés 272 Date d'inscription mardi 27 novembre 2007 Statut Membre Dernière intervention 14 janvier 2011 6
26 juin 2009 à 00:05
quand je l'enlève ça renommer plus si il excite deja
0
Dalida Messages postés 6728 Date d'inscription mardi 14 mai 2002 Statut Contributeur Dernière intervention 11 janvier 2016 920
26 juin 2009 à 00:12
il t'affiche quoi comme nom de fichier ?
0
aureliendu917 Messages postés 272 Date d'inscription mardi 27 novembre 2007 Statut Membre Dernière intervention 14 janvier 2011 6
26 juin 2009 à 00:14
sa m'affiche le mon d'origine de l'images par exemple "images.png"
0
Dalida Messages postés 6728 Date d'inscription mardi 14 mai 2002 Statut Contributeur Dernière intervention 11 janvier 2016 920
26 juin 2009 à 00:16
while(file_exists($target_path))
{
	$name = pathinfo($target_path, PATHINFO_FILENAME);
	$ext = pathinfo($target_path, PATHINFO_EXTENSION);
	$target_path = $name.rand(0, 9).'.'.$ext;
	var_dump($target_path);
}

ça ?

je viens de le tester avec un "index.php" existant et il me renvoie "index1.php"…
0
aureliendu917 Messages postés 272 Date d'inscription mardi 27 novembre 2007 Statut Membre Dernière intervention 14 janvier 2011 6
26 juin 2009 à 00:19
sa copie et ça renommer bien mai il envoie pas le fichier dans le dossier "uploads" !
0
Dalida Messages postés 6728 Date d'inscription mardi 14 mai 2002 Statut Contributeur Dernière intervention 11 janvier 2016 920
26 juin 2009 à 00:22
ah ben tu vois que tu peux être clair des fois !
-:oD

je te dirais bien de faire un petit "var_dump($_FILES);" pour savoir ce que le serveur reçoit comme fichier…
0
aureliendu917 Messages postés 272 Date d'inscription mardi 27 novembre 2007 Statut Membre Dernière intervention 14 janvier 2011 6
26 juin 2009 à 00:25
sa renvoie ça :

array(1) { ["uploadedfile"]=>  array(5) { ["name"]=>  string(13) "bannieres.png" ["type"]=>  string(9) "image/png" ["tmp_name"]=>  string(23) "C:\wamp\tmp\php9504.tmp" ["error"]=>  int(0) ["size"]=>  int(70830) } } The file bannieres.png has been uploaded
0
Dalida Messages postés 6728 Date d'inscription mardi 14 mai 2002 Statut Contributeur Dernière intervention 11 janvier 2016 920
26 juin 2009 à 00:29
et tu as regardé ailleurs que dans "uploads" ?
genre un dossier ou deux au dessus ?

parce que le fichier existe bien et il le déplace bien des temporaires vars ailleurs, reste à trouver le ailleurs…
0