Page 1 sur 1

upload avatar

Posté : 05 janv. 2014, 16:56
par cris84100
Bonjour à tous,

je voudrais uploader un avatar depuis un formulaire avec FILE et non par une URL comme actuellement.
Ci-dessous le code utilisé

merci de vos remarques et idées
	<?php if(isset($_POST['avatar_actif'], $_POST['avatar']))
	{
		//On vérifie si l'avatar est bon
		$avatar_actif = intval($_POST['avatar_actif']);
		$avatar = mysql_real_escape_string(htmlentities($_POST['avatar']));
		$infos_avatar = getimagesize($avatar);
		
		if($avatar_actif == 1)//si l'on met un nouvel avatar
		{
			//verif avatar
			if($infos_avatar['0'] <= $config['largeur_max_avatar'])
			{
				if($infos_avatar['1'] <= $config['hauteur_max_avatar'])
				{
					mysql_query('UPDATE '.$prefix.'membres SET avatar_actif=1, avatar=\''.$avatar.'\' WHERE id='.$_SESSION[$prefix.'id'])or die(mysql_error());
					header('location: profil.php?id='.$_SESSION[$prefix.'id']);
?>

Re: upload avatar

Posté : 06 janv. 2014, 00:17
par cyruss
Salut,

techniquement c'est pas très compliqué. Il faut que tu ajoute un champs d'upload dans ton formulaire.
Ensuite tu récupères ton fichier avec move_uploaded_file (http://php.net/manual/fr/function.move- ... d-file.php) et je te conseille de le renommer avec un identifiant unique (par exemple l'identifiant du user concerné).

Re: upload avatar

Posté : 06 janv. 2014, 16:14
par cris84100
bonjour,$j'avoue avoir essayé tout un tas de formules mais sans succès, à chaque fois l'upload se passe bien mais rien dans le dossier images

Techniquement, je pense que je ne suis pas assez calé en php pour pouvoir faire une telle chose.
Merci de votre aide.
Voici le formulaire basique d'envoi de l'avatar et je mets le code php en entier si quel'un peut m'aider..
<html>
  <body>
    <form enctype="multipart/form-data" action="upload.php" method="post">
      <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
      Transfère le fichier <input type="file" name="avatar" />
      <input type="submit" />
    </form>
  </body>
</html>
<?php
if (isset($_SESSION[$prefix.'logged']) && $_SESSION[$prefix.'logged'] === true)
{
	if(isset($_POST['avatar_actif'], $_POST['avatar']))
	{
		//On vérifie si l'avatar est bon
		$avatar_actif = intval($_POST['avatar_actif']);
		$avatar = mysql_real_escape_string(htmlentities($_POST['avatar']));
		$infos_avatar = getimagesize($avatar);
		
		if($avatar_actif == 1)//si l'on met un nouvel avatar
		{
			//verif avatar
			if($infos_avatar['0'] <= $config['largeur_max_avatar'])
			{
				if($infos_avatar['1'] <= $config['hauteur_max_avatar'])
				{
					mysql_query('UPDATE '.$prefix.'membres SET avatar_actif=1, avatar=\''.$avatar.'\' WHERE id='.$_SESSION[$prefix.'id'])or die(mysql_error());
					header('location: profil.php?id='.$_SESSION[$prefix.'id']);
				}
				else
				{
					echo stop('Erreur : votre image ets trop haute.  La hauteur maximale autoris&eacute;e est de '.$config['hauteur_max_avatar'].' pixels.', 'modifier_profil.php?action=modifier_avatar');
				}
			}
			else
			{
				echo stop('Erreur : votre image ets trop large.  La largeur maximale autoris&eacute;e est de '.$config['largeur_max_avatar'].' pixels.', 'modifier_profil.php?action=modifier_avatar');
			}
		}
		else//si l'on désactive juste
		{
			mysql_query('UPDATE '.$prefix.'membres SET avatar_actif=0, avatar=\''.$avatar.'\' WHERE id='.$_SESSION[$prefix.'id'])or die(mysql_error());
			header('location: profil.php?id='.$_SESSION[$prefix.'id']);
		}	
	}
?>

Re: upload avatar

Posté : 06 janv. 2014, 16:36
par Mazarini
Pour les upload, il faut utiliser $_FILES (cf http://www.php.net/manual/en/reserved.v ... .files.php)

Re: upload avatar

Posté : 06 janv. 2014, 17:32
par cris84100
Oui je sais mais j'arrive pas à faire l'adaptation avec mon code.
L'upload se passe bien mais l'image de va pas dans le fichier de destination, j'avais utilisé ceci :
<?php
if(isset($_FILES['uploaded'])){
$target = "galleries/".basename($_FILES['uploaded']['name']) ;


if(move_uploaded_file($_FILES['uploaded']['tmp_name'],$target)) echo "OK!";

}
?>