Page 1 sur 1

Message d'erreur quand il n'y a pas de fichier

Posté : 30 mars 2014, 13:53
par ISoon
bonjour,

J'ai se code pour upload un fichier et ajouter son lien dans ma base de donnée :
<form method="post" action="creabattle.php?page=page2" enctype="multipart/form-data">
 
<br/><br/>
 <input type="hidden" name="MAX_FILE_SIZE" value="300000" >
 <div style="float:left;margin-left:50px;"><input type="file" accept="image/png" id="file" name="image"></div>
  <div style="float:right;margin-right:50px;"><input type="file" accept="image/png" id="file" name="image2"></div><br/><br/><br/><br/>
 <input type="submit" style="font-size:22px;height:50px;border:1px solid #7CFC00;background-color:#7CFC00; width:500px; border-radius:7px;" name="envoyer" value="Ajouter">
 </li>
 </ul>
</form>
</center></div>
                       <?php

                    }
					 if($page == 'page2') 
					{
					
					?>
<?php

// ***** ici on récupère les données et on les stocke dans mysql

 
//******* On renomme l'image de manière aléatoire pour éviter un conflit dans le dossier (2 fois le même nom par exemple
$dir = 'upload/1/';
$ext = $_FILES['image']['name'];

$png = '.png';
$file=uniqid().'.'.$ext .$png;

$dir2 = 'upload/2/';
$ext2 = $_FILES['image2']['name'];
$file2=uniqid().'.'.$ext2 .$png;
 //**** on bouge l'image
move_uploaded_file($_FILES['image']['tmp_name'], $dir.$file);
move_uploaded_file($_FILES['image2']['tmp_name'], $dir2.$file2); 


$photo12 = "http://thebattlepic.com/upload/1/".$file."";
$photo22 = "http://thebattlepic.com/upload/2/".$file2."";


if(isset($_POST['username'],$_POST['password'],$_POST['email']))
		{
			$username = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8');
			if($_POST['password']==$_POST['passverif'])
			{
				$password = htmlentities($_POST['password'], ENT_QUOTES, 'UTF-8');
			}
			else
			{
				$password = '';
			}
			$email = htmlentities($_POST['email'], ENT_QUOTES, 'UTF-8');
			$avatar = htmlentities($_POST['avatar'], ENT_QUOTES, 'UTF-8');
		}
		else
		{
			//Sinon, on affiche les donnes a partir de la base de donnee
			$dnn = mysql_fetch_array(mysql_query('select * from users where username="'.$_SESSION['username'].'"'));
			$id = htmlentities($dnn['id'], ENT_QUOTES, 'UTF-8');
			$username = htmlentities($dnn['username'], ENT_QUOTES, 'UTF-8');
			$password = htmlentities($dnn['password'], ENT_QUOTES, 'UTF-8');
			$email = htmlentities($dnn['email'], ENT_QUOTES, 'UTF-8');
			$avatar = htmlentities($dnn['avatar'], ENT_QUOTES, 'UTF-8');
		}


$id_membre = $id;

$sql = "INSERT INTO duel(photo1, photo2,id_membre)";
$sql .= "VALUES('$photo12', '$photo22','$id_membre')";
$result = mysql_query($sql) or die(mysql_error());

?>	
Mais est ce que c'est possible que si on upload aucun fichier et quand on appuie sur ajouter, il y a un message d'erreur?

Merci beaucoup

Re: Message d'erreur quand il n'y a pas de fichier

Posté : 30 mars 2014, 14:14
par michel71
Bonjour,

tester si $ext contient quelque chose et si non "echo 'bla bla'

Re: Message d'erreur quand il n'y a pas de fichier

Posté : 05 avr. 2014, 12:49
par ISoon
Bonjour,

tester si $ext contient quelque chose et si non "echo 'bla bla'
Voici se que j'ai mis :
<form method="post" action="creabattle.php?page=page2" enctype="multipart/form-data">
 
<br/><br/>
 <input type="hidden" name="MAX_FILE_SIZE" value="300000" >
 <div style="float:left;margin-left:50px;"><input type="file" accept="image/png" id="file" name="image"></div>
  <div style="float:right;margin-right:50px;"><input type="file" accept="image/png" id="file" name="image2"></div><br/><br/><br/><br/>
 <input type="submit" style="font-size:22px;height:50px;border:1px solid #7CFC00;background-color:#7CFC00; width:500px; border-radius:7px;" name="envoyer" value="Ajouter">
 </li>
 </ul>
</form>
</center></div>
                       <?php

                    }
					 if($page == 'page2') 
					{
					
					?>
<?php
$image = htmlspecialchars(mysql_escape_string($_POST['image']));
$image2 = htmlspecialchars(mysql_escape_string($_POST['image2']));


if (!empty($image & $image2 ))// Si l'entrée du tableau données n'est pas vide alors execute les accolades suivantes
        {
        echo' Vous devez insérer des images';
      
        }else {
 
//******* On renomme l'image de manière aléatoire pour éviter un conflit dans le dossier (2 fois le même nom par exemple
$dir = 'upload/1/';
$ext = $_FILES['image']['name'];

$png = '.png';
$file=uniqid().'.'.$ext .$png;

$dir2 = 'upload/2/';
$ext2 = $_FILES['image2']['name'];
$file2=uniqid().'.'.$ext2 .$png;
 //**** on bouge l'image
move_uploaded_file($_FILES['image']['tmp_name'], $dir.$file);
move_uploaded_file($_FILES['image2']['tmp_name'], $dir2.$file2); 


$photo12 = "http://thebattlepic.com/upload/1/".$file."";
$photo22 = "http://thebattlepic.com/upload/2/".$file2."";



if(isset($_POST['username'],$_POST['password'],$_POST['email']))
		{
			$username = htmlentities($_POST['username'], ENT_QUOTES, 'UTF-8');
			if($_POST['password']==$_POST['passverif'])
			{
				$password = htmlentities($_POST['password'], ENT_QUOTES, 'UTF-8');
			}
			else
			{
				$password = '';
			}
			$email = htmlentities($_POST['email'], ENT_QUOTES, 'UTF-8');
			$avatar = htmlentities($_POST['avatar'], ENT_QUOTES, 'UTF-8');
		}
		else
		{
			//Sinon, on affiche les donnes a partir de la base de donnee
			$dnn = mysql_fetch_array(mysql_query('select * from users where username="'.$_SESSION['username'].'"'));
			$id = htmlentities($dnn['id'], ENT_QUOTES, 'UTF-8');
			$username = htmlentities($dnn['username'], ENT_QUOTES, 'UTF-8');
			$password = htmlentities($dnn['password'], ENT_QUOTES, 'UTF-8');
			$email = htmlentities($dnn['email'], ENT_QUOTES, 'UTF-8');
			$avatar = htmlentities($dnn['avatar'], ENT_QUOTES, 'UTF-8');
		}


$id_membre = $id;

$sql = "INSERT INTO duel(photo1, photo2,id_membre)";
$sql .= "VALUES('$photo12', '$photo22','$id_membre')";
$result = mysql_query($sql) or die(mysql_error());


}
?>		
Mais j'ai une page blanche, qu'elle est mon erreur?

Merci beaucoup