Bonjour a tous.
Débutant en php, j'ai fait un code d'upload de photo, avec deux façon(code récupérer sur différent forum) de tester l'extension et la taille du fichier.
le contrôle de l'extension se déroule parfaitement bien dans les 2 cas, par contre pour la taille des fichiers rien ne se produit. pour le premier test (avatar) il me retourne le message d'erreur "erreur lors du transfert" hors qu'il devrait me retourner "Fichier trop gros", et pour le deuxième il continue comme si de rien était en ne me mettant aucun message d'erreur. après de longue et infructueuse recherche, je n'arrive passe a voir ou ça cloche.

2eme question en cas d'erreur ,a l'heure actuel, ça "reset" la page(efface tout les champs) est-il possible de garder en mémoire les chemins déjà sélectionné et valide, et n'effacer que les chemins avec mauvaise extention ou trop gros ?
d'avance merci
mon code :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
session_start();
mb_http_output("utf-8");
$compte = $_SESSION['compte'];
require('connect.php');
$max_size = 1048576;
if(!empty($_FILES))
{
	$avatar = $_FILES['avatar'];
	$avatar_name = $avatar['name'];
	$ext = strtolower(substr(strrchr($avatar_name,'.'),1));
	$ext_aut = array('jpg','jpeg');
 
	function check_extension($ext,$ext_aut)
	{
		if(in_array($ext,$ext_aut))
		{
			return true;
		}
	}
	//test extension
	$valid = (!check_extension($ext,$ext_aut)) ? false : true;
	$erreur = (!check_extension($ext,$ext_aut)) ? 'Veuillez selectionner une image en .jpg ou .jpeg' : '';
	//test taille
	if($valid)
	{   	    
		if($avatar['size']>$max_size)
		{
			$valid = false;
			$erreur = 'Fichier trop gros';
		}
	}
 
	if($valid)
	{
		if($avatar['error']>0)
		{
			$valid = false;
			$erreur = 'Erreur lors du transfert';
		}
	}
 
	if ($valid)
	{
	$path_to_image = 'photos/fullsize/';
	$path_to_portrait = 'photos/portraits/';
    $path_to_min = 'photos/mini/';
 
	$filename = sha1(uniqid($avatar_name));	
 
	$source = $avatar['tmp_name'];
	$target = $path_to_image . $filename. '.'. $ext;
 
    move_uploaded_file($source,$target);
 
	if($ext == 'jpg' || $ext == 'jpeg') {$im = imagecreatefromjpeg($path_to_image.$filename.'.'.$ext);}
	$ox = imagesx($im);
	$oy = imagesy($im);
 
	$nx = 240;
	$ny = floor($oy *($nx/$ox));
	$nm = imagecreatetruecolor($nx,$ny);
 
		imagecopyresized($nm, $im, 0,0,0,0, $nx,$ny,$ox,$oy);
 
		imagejpeg($nm, $path_to_portrait.$filename.'.'.$ext);
 
		$nom_image = $filename.'.'.$ext;
	$nx = 80;
	$ny = floor($oy *($nx/$ox));
	$nm = imagecreatetruecolor($nx,$ny);
 
		imagecopyresized($nm, $im, 0,0,0,0, $nx,$ny,$ox,$oy);
 
		imagejpeg($nm, $path_to_min.$filename.'.'.$ext);
 
 
		$nom_image = $filename.'.'.$ext;	
	    $req = $bdd->prepare('INSERT INTO avatar_upload(avatar,compte) VALUES(:avatar,:compte)')or die(print_r($bdd->errorInfo()));
 
		$req->execute(array(':avatar'=>$nom_image,
		                    ':compte'=>$compte));
		$req->closeCursor();			
 
 
 
 
 foreach($_FILES["photo"]["name"] as $key => $name)
 
       {
 
      // Définition des variables
      $photo = $_FILES['photo'];
      $photo_name = $photo['name'][$key];
      $file_tmp =$_FILES['photo']['tmp_name'][$key];
      $file_type=$_FILES['photo']['type'][$key];   
      $ext2 = strtolower(substr(strrchr($_FILES["photo"]["name"][$key],'.'),1));
      $ext_aut2 = array('jpg','jpeg');
      $size2 = $photo['size'][$key];
      //definition du titre 
	  $titre =$_POST['titre'][$key];  
 
      //si input vide on passe
 
      if(!$_FILES["photo"]["name"][$key]) continue;
 
 
      //test de l'extension
 
          if(!in_array($ext2, $ext_aut2)) //Si l'extension n'est pas dans le tableau
           {
             $erreur2 = 'Veuillez selectionner une image en .jpg ou .jpeg';//on créer une variable erreur
           }
 
    // test de la taille
 
         if($size2 > $max_size)//Si La taille est trop grande  
           {
              $erreur2 = 'Le fichier est trop gros...';// on définit un erreur pour la taille
           }
 
         if(!isset($erreur2))//Si il n'y a pas d'erreur
 
    //traitement de la photo
           {
          $path_to_image2 = 'photos/fullsize/';
          $path_to_min2 = 'photos/mini/';
    //renommage de la photo
          $filename2 = sha1(uniqid($photo_name[$key])); 
 
          $source2 = $photo['tmp_name'][$key];
          $target2 = $path_to_image2 . $filename2. '.'. $ext2;
   // deplacement de la photo dans fullsize
          move_uploaded_file($source2,$target2);
 
          if($ext2 == 'jpg' || $ext2 == 'jpeg') {$im2 = imagecreatefromjpeg($path_to_image2.$filename2.'.'.$ext2);}
 
          $ox2 = imagesx($im2);
          $oy2 = imagesy($im2);
 
    // reduction de la photo
        $nom_image2 = $filename2.$key.'.'.$ext2;
        $nx2 = 80;
        $ny2 = floor($oy2 *($nx2/$ox2));
        $nm2 = imagecreatetruecolor($nx2,$ny2);
 
        imagecopyresized($nm2, $im2, 0,0,0,0, $nx2,$ny2,$ox2,$oy2);
 
        imagejpeg($nm2, $path_to_min2.$filename2.'.'.$ext2);
 
 
    // insertion dans la base de donnée
        $req = $bdd->prepare('INSERT INTO upload(compte,photo'.$key.',titre'.$key.') VALUES(:compte,:photo,:titre)');
        $req->execute(array('compte'=>$compte,'photo'=>$nom_image2,'titre'=>$titre));
        $req->closeCursor();
 
   // si pas d'erreur rediriger vers la fin du formulaire
 
   }
 
        }
		if(!isset($erreur2)){
      header("Location:envoi.php") or die();
	   }
	   }
}
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
 <link rel="stylesheet" href="stylecreation.css" />
 <link rel="shortcut icon" href="images/logo.ico" type="image/x-icon" />
 </head>
<body>
<header>
<div id="titre_principal">
<img src="images/titre.png" alt="titre" id="titre" />
</div>
</header> 
   <section >
 
     <form id=photo method="post"  enctype="multipart/form-data" action=""/> 
     <input type="hidden" name="MAX_FILE_SIZE" value="1048576" />   
     <p class="double">
     <h3>Photo (obligatoire) </h3>
     <input type="file" name="avatar" id="avatar" /></br>
     </p>
<?php if(isset($erreur)):?>
<div class="error"><?php echo $erreur;?></div>
<?php endif;?>
 
 
       <h3>Photos de l'album photo (facultative)</h3>
       <?php if(isset($erreur2)):?>
       <div class="error"><?php echo $erreur2;?></div>
       <?php endif; ?>
       <p class="double">
       <input type="file" name="photo[]" /><label>Titre</label><input type="text" name="titre[]" /><br/>
       <input type="file" name="photo[]" /><label>Titre</label><input type="text" name="titre[]" /><br/>
       <input type="file" name="photo[]" /><label>Titre</label><input type="text" name="titre[]" /><br/>
       <input type="file" name="photo[]" /><label>Titre</label><input type="text" name="titre[]" /><br/>
       <input type="file" name="photo[]" /><label>Titre</label><input type="text" name="titre[]" /><br/>
       <input type="file" name="photo[]" /><label>Titre</label><input type="text" name="titre[]" /><br/>
       <input type="file" name="photo[]" /><label>Titre</label><input type="text" name="titre[]" /><br/>
       <input type="file" name="photo[]" /><label>Titre</label><input type="text" name="titre[]" /><br/>
       <input type="file" name="photo[]" /><label>Titre</label><input type="text" name="titre[]" /><br/>
       <input type="file" name="photo[]" /><label>Titre</label><input type="text" name="titre[]" /><br/>
       <input type="file" name="photo[]" /><label>Titre</label><input type="text" name="titre[]" /><br/>
       <input type="file" name="photo[]" /><label>Titre</label><input type="text" name="titre[]" /><br/>
       <input type="file" name="photo[]" /><label>Titre</label><input type="text" name="titre[]" /><br/>
       <input type="file" name="photo[]" /><label>Titre</label><input type="text" name="titre[]" /><br/>
       <input type="file" name="photo[]" /><label>Titre</label><input type="text" name="titre[]" /><br/>
       <input type="file" name="photo[]" /><label>Titre</label><input type="text" name="titre[]" /><br/>
       <input type="file" name="photo[]" /><label>Titre</label><input type="text" name="titre[]" /><br/>
       <input type="file" name="photo[]" /><label>Titre</label><input type="text" name="titre[]" /><br/>
       <input type="file" name="photo[]" /><label>Titre</label><input type="text" name="titre[]" /><br/>
       <input type="file" name="photo[]" /><label>Titre</label><input type="text" name="titre[]" /><br/>
       </p>
 
       <input type="submit" class="submit" name="submit" value="Ajouter"/><br />
 
 
        </form>
    </section>
</body>
</html>