bonjour a tous.
je débute en php, j'essai de faire un script pour que l'utilisateur puisse modifier
ces contenues déjà uploader.
pour cela j'avait fais une boucle :
Code php : 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
// Pour chaque input
for($i=0;$i<20;$i++)
     {
 
      // Définition des variables
      $photo = $_FILES['photo'];
      $photo_name = $photo['name'][$i];
      $file_tmp =$_FILES['photo']['tmp_name'][$i];
      $file_type=$_FILES['photo']['type'][$i];   
      $ext2 = strtolower(substr(strrchr($_FILES["photo"]["name"][$i],'.'),1));
      $ext_aut2 = array('jpg','jpeg');
      $size2 = $photo['size'][$i];
	  $taillemaxi = 650;
 
      //definition du titre 
	  $titre =$_POST['titre'][$i];  
 
      //si input vide on passe
 
      if(!$_FILES["photo"]["name"][$i]) 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 = 'un des fichiers 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_temp = 'photos/temp/';
          $path_to_image2 = 'photos/fullsize/';
          $path_to_min2 = 'photos/mini/';
    //renommage de la photo
          $filename2 = sha1(uniqid($photo_name)); 
          $filename2 = "$filename2.$ext2";
		  $nom_image2 = $filename2;
          $source2 = $file_tmp;
          $target2 = $path_to_temp.$nom_image2;
		  $targetfull = $path_to_image2.$nom_image2;
   // deplacement de la photo dans fullsize
          move_uploaded_file($source2,$target2);
 
          if($ext2 == 'jpg' || $ext2 == 'jpeg') {$im2 = imagecreatefromjpeg($path_to_temp.$filename2);}
 
		  $ox2 = imagesx($im2);
          $oy2 = imagesy($im2);
    //orientation de la photo
 
    if($ox2>=$oy2)//si la photo est en paysage
 
     {  
	     if ($ox2>=$taillemaxi) //si la photo fait plus de 487 pixel de large elle est redimenssionnée
	    { 
          $nx2 = $taillemaxi;
          $ny2 = ($taillemaxi*$oy2)/$ox2;
		  $nm2 = imagecreatetruecolor($nx2,$ny2);
		  imagecopyresized($nm2, $im2, 0,0,0,0, $nx2,$ny2,$ox2,$oy2);
	      imagejpeg($nm2, $path_to_image2.$filename2);
 
        }
 
	    else //sinon la copier dans le repertroire fullsize
		{ 
			 copy($target2,$targetfull);
	    }
	 }
	else //si la photo est en portrait
	 { 
	     if ($oy2>$taillemaxi) //si la photo fait plus de 487 pixel de haut elle est redimenssionnée
	     {  
	     $nx2 = ($taillemaxi*$ox2)/$oy2;
	     $ny2= $taillemaxi;
		 $nm2 = imagecreatetruecolor($nx2,$ny2);
		 imagecopyresized($nm2, $im2, 0,0,0,0, $nx2,$ny2,$ox2,$oy2);
	     imagejpeg($nm2, $path_to_image2.$filename2);
 
	     }
 
	     else //sinon la copier dans le repertroire fullsize
		 { 
	      copy($target2,$targetfull);
	     }
	 }
 
 
 
    // creation de la miniature
 
        $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);
 
    //suppression des photos
 
       unlink("photos/temp/".$filename2);
 
	// insertion dans la base de donnée
        $req2 = $bdd->prepare('UPDATE upload SET photo'.$i.'=:photo,titre'.$i.'= :titre WHERE compte = :compte');
        $req2->execute(array('photo'=>$nom_image2,'titre'=>$titre,':compte'=>$compte));
        $req2->closeCursor();
 
   // si pas d'erreur rediriger vers la fin du formulaire
 
   }
 
        }
		if(!isset($erreur2)){
session_destroy();		
 
header("Location:envoi.php") or die();
	  }
	   }
 
	}
mon souci c'est si l'utilisateur veut modifier une photo (en uploader une autre,)j'aimerais pouvoir en meme temps supprimer ces photos dans les dossiers.
et la je ne voit pas comment faire, pour supprimer par exemple la 3eme photos sans effacer les autres. la seule solution que je vois est de faire un test a chaque fois pour vérifier si il existe une photo ou pas et donc de répéter l'opération 20 fois avec a chaque fois le script de tratement de photo..
j'espere avoir été assez clair. je vous jointmon formulaire de modif,
si vous avez une meilleure idée je sus preneur!!!
d'avance merci
Code php : 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php
require('connect.php');
error_reporting(E_ALL);
session_start();
 
$max_size = 1048576;
 
//recuperation des données
if((!empty ($_SESSION['compte'])) && (!empty ($_SESSION['mail_createur'])))
{
$id = $_SESSION['compte'];
$mail_createur = $_SESSION['mail_createur'];
$req = $bdd->prepare('SELECT * FROM creation JOIN avatar_upload ON id = avatar_upload.compte JOIN upload ON upload.compte = avatar_upload.compte WHERE id = :id')or die(print_r($bdd->errorInfo()));
$req->execute(array(':id'=>$id));
$donnees = $req->fetch();
$req->closeCursor();
 
$Oldavatar=$donnees['avatar'];
 
if(!empty($_FILES))
	{
//récupération des données saisie:
$nom_f=$_POST['nom_f'];
$surnom=$_POST['surnom'];
$pays=$_POST['pays'];
$textsaisie=$_POST['textsaisie'];
$signature=$_POST['signature'];
 
	$req2 = $bdd->prepare('UPDATE creation SET nom_f=:nom_f,surnom=:surnom, pays=:pays,textsaisie=:textsaisie,signature=:signature WHERE id = :id');
        $req2->execute(array(':nom_f'=>$nom_f,':surnom'=>$surnom,':pays'=>$pays,':textsaisie'=>$textsaisie,':signature'=>$signature,':id'=>$id));
        $req2->closeCursor();
 
 
		//test modif photo avatar
 
	if($_FILES['avatar'] !="")
	{
 
		if(!empty($donnees['avatar']))
 
		{
			unlink("photos/mini/".$Oldavatar);
			unlink("photos/portraits/".$Oldavatar);
 
		}
 
	$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/temp/';
	$path_to_portrait = 'photos/portraits/';
    $path_to_min = 'photos/mini/';
 
	$filename = sha1(uniqid($avatar_name));	
 
	$source = $avatar['tmp_name'];
	$fichier =$filename. '.'. $ext;
	$target = $path_to_image .$fichier;
	$targetpetit = $path_to_portrait.$fichier;
	//deplacement du fichier dans le repertoire temporaire
    move_uploaded_file($source,$target);
 
	if($ext == 'jpg' || $ext == 'jpeg') {$im = imagecreatefromjpeg($path_to_image.$filename.'.'.$ext);}
	$ox = imagesx($im);
	$oy = imagesy($im);
	if ($ox>=240)//si laphoto depasse 240 px la reduire
	    {
	$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;
	    }
	else //sinon la deplacer directement dans portraits
     	{
	copy($target,$targetpetit);	
	    }
	$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('UPDATE avatar_upload SET avatar = :avatar WHERE compte=:compte')or die(print_r($bdd->errorInfo()));
				$req->execute(array(':avatar'=>$nom_image,
		                            ':compte'=>$id));
		        $req->closeCursor();			
 
        unlink("photos/temp/".$fichier);
 
	     }
		if(!isset($erreur2))
	     {
 
		//fermeture de la session	
		header("Location:modifok.php") or die();
	     }
 
 
	   }
	}
}
else
{
session_destroy();
header("location:index.php");	
}
 
?>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=charset=ISO-8859-1" />
<title></title>
<link rel="stylesheet" href="style.css" type="text/css" charset="charset=ISO-8859-1" />
<link rel="shortcut icon" href="images/logo.ico" type="image/x-icon" />
<!--[if lte IE 7]> <html class="ie67 ie678" lang="fr"> <![endif]-->
<!--[if IE 8]> <html class="ie8 ie678" lang="fr"> <![endif]-->
<!--[if gt IE 8]><!--> <html lang="fr"> <!--<![endif]-->
<!--[if lt IE 9]>
		<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
	<![endif]-->
<script language="javascript">
 <!--
                        function pop()
                        {
                                width = 1000;
                                height = 700;
                                if(window.innerWidth)
                                {
                                        var left = (window.innerWidth-width)/2;
                                        var top = (window.innerHeight-height)/2;
                                }
                                else
                                {
                                        var left = (document.body.clientWidth-width)/2;
                                        var top = (document.body.clientHeight-height)/2;
                                }
                                window.open('popup_photo.html','pop','menubar=no, scrollbars=yes, top='+top+', left='+left+', width='+width+', height='+height+'');
                        }
</script>
</head>
 
<body>
<header>
<div id="titre_principal"> <a href="index.php"><img src="images/titre.png" alt="titre" id="titre" /></a>
</div>
</header>
<section> 
<article >
<h3 style="text-align:center">Veuillez modifier les champs</h3> 
<p>Nom: <?php echo $donnees['nom'];?><br/>
Prénom : <?php echo $donnees['prenom'];?></p>
<form id="form1" enctype="multipart/form-data" method="post" action="">
<p>
<label >Nom de jeune fille: </label> <input type="text" name="nom_f" value="<?php if (!empty($donnees['nom_f'])){echo $donnees['nom_f'];} ?>" /></p>
<p>
<label >Surnom: </label> <input type="text" name="surnom" value="<?php if (!empty($donnees['surnom'])){echo $donnees['surnom'];} ?>" /></p>
<p>
<label for="ville">Pays:</label> <input type="text" name="pays" value="<?php if (!empty($donnees['pays'])) {echo $donnees['pays'];}?>"/></p>
<p>
<textarea name="textsaisie" rows="10" cols="55"><?php echo $donnees['textsaisie']?>  </textarea></p>
<p>
<label>Signature:</label> <input type="text" name="signature" value="<?php if (!empty($donnees['signature'])) {echo $donnees['signature'];}?>"/></p>
 
 
<h3 style="text-align:center;">Les photos</h3>
<p style="text-align:center;">La taille des photos ne doit pas dépasser 1 m&eacute;gaoctet. Si vos photos d&eacute;passent 1 m&eacute;gaoctet et que vous ne savez pas comment les     r&eacute;duire, cliquez sur le lien ci dessous<br/>
 
     <input id="form1" type="button" name="reduirephoto" value="Reduire ses photos" onClick="javascript:pop()"  /></br></br>
</p>
<p>Modification de la photo de l'espace<br/>
<?php if (!empty($Oldavatar)){ echo"<img src=\"photos/mini/$Oldavatar\" alt=\"avatar\" />";}?><input type="file" name="avatar" />
</p>
<p>Modification de l'album photo et des titres :<br/> 
 
<?php 
 
//les variables photo et titre
$photo0 = $donnees['photo0']; $titre0 = $donnees['titre0'];
$photo1 = $donnees['photo1']; $titre1 = $donnees['titre1'];
$photo2 = $donnees['photo2']; $titre2 = $donnees['titre2'];
$photo3 = $donnees['photo3']; $titre3 = $donnees['titre3'];
$photo4 = $donnees['photo4']; $titre4 = $donnees['titre4'];
$photo5 = $donnees['photo5']; $titre5 = $donnees['titre5'];
$photo6 = $donnees['photo6']; $titre6 = $donnees['titre6'];
$photo7 = $donnees['photo7']; $titre7 = $donnees['titre7'];
$photo8 = $donnees['photo8']; $titre8 = $donnees['titre8'];
$photo9 = $donnees['photo9']; $titre9 = $donnees['titre9'];
$photo10 = $donnees['photo10']; $titre10 = $donnees['titre10'];
$photo11 = $donnees['photo11']; $titre11 = $donnees['titre11'];
$photo12 = $donnees['photo12']; $titre12 = $donnees['titre12'];
$photo13 = $donnees['photo13']; $titre13 = $donnees['titre13'];
$photo14 = $donnees['photo14']; $titre14 = $donnees['titre14'];
$photo15 = $donnees['photo15']; $titre15 = $donnees['titre15'];
$photo16 = $donnees['photo16']; $titre16 = $donnees['titre16'];
$photo17 = $donnees['photo17']; $titre17 = $donnees['titre17'];
$photo18 = $donnees['photo18']; $titre18 = $donnees['titre18'];
$photo19 = $donnees['photo19']; $titre19 = $donnees['titre19'];
?>
Photo 1 :<br/>
<?php 
if (!empty($photo0)){ echo"<img src=\"photos/mini/$photo0\" alt=\"$titre0>\"/>";}?><input type="file" name="NewPhoto[]" />
Titre<input type="text" name="Titre[]" value="<?php if (!empty($titre0)) {echo $titre0;}?>"/><br/>
Photo 2 :<br/>
<?php if (!empty($photo1)){ echo"<img src=\"photos/mini/$photo1\" alt=\"$titre1>\"/>";}?><input type="file" name="NewPhoto[]" />
Titre<input type="text" name="Titre[]" value="<?php if (!empty($titre1)) {echo $titre1;}?>"/><br/>
Photo 3 :<br/>
<?php if (!empty($photo2)){ echo"<img src=\"photos/mini/$photo2\" alt=\"$titre2>\"/>";}?><input type="file" name="NewPhoto[]" />
Titre<input type="text" name="Titre[]" value="<?php if (!empty($titre2)) {echo $titre2;}?>"/><br/>
Photo 4 :<br/>
<?php 
if (!empty($photo3)){ echo"<img src=\"photos/mini/$photo3\" alt=\"$titre3>\"/>";}?><input type="file" name="NewPhoto[]" />
Titre<input type="text" name="Titre[]" value="<?php if (!empty($titre3)) {echo $titre3;}?>"/><br/>
Photo 5 :<br/>
<?php 
if (!empty($photo4)){ echo"<img src=\"photos/mini/$photo4\" alt=\"$titre4>\"/>";}?><input type="file" name="NewPhoto[]" />
Titre<input type="text" name="Titre[]" value="<?php if (!empty($titre4)) {echo $titre4;}?>"/><br/>
Photo 6 :<br/>
<?php 
if (!empty($photo5)){ echo"<img src=\"photos/mini/$photo5\" alt=\"$titre5>\"/>";}?><input type="file" name="NewPhoto[]" />
Titre<input type="text" name="Titre[]" value="<?php if (!empty($titre5)) {echo $titre5;}?>"/><br/>
Photo 7 :<br/>
<?php 
if (!empty($photo6)){ echo"<img src=\"photos/mini/$photo6\" alt=\"$titre6>\"/>";}?><input type="file" name="NewPhoto[]" />
Titre<input type="text" name="Titre[]" value="<?php if (!empty($titre6)) {echo $titre6;}?>"/><br/>
Photo 8 :<br/>
<?php 
if (!empty($photo7)){ echo"<img src=\"photos/mini/$photo7\" alt=\"$titre7>\"/>";}?><input type="file" name="NewPhoto[]" />
Titre<input type="text" name="Titre[]" value="<?php if (!empty($titre7)) {echo $titre7;}?>"/><br/>
Photo 9 :<br/>
<?php 
if (!empty($photo8)){ echo"<img src=\"photos/mini/$photo8\" alt=\"$titre8>\"/>";}?><input type="file" name="NewPhoto[]" />
Titre<input type="text" name="Titre[]" value="<?php if (!empty($titre8)) {echo $titre8;}?>"/><br/>
Photo 10 :<br/>
<?php 
if (!empty($photo9)){ echo"<img src=\"photos/mini/$photo9\" alt=\"$titre9>\"/>";}?><input type="file" name="NewPhoto[]" />
Titre<input type="text" name="Titre[]" value="<?php if (!empty($titre9)) {echo $titre9;}?>"/><br/>
Photo 11 :<br/>
<?php 
if (!empty($photo10)){ echo"<img src=\"photos/mini/$photo10\" alt=\"$titre10>\"/>";}?><input type="file" name="NewPhoto[]" />
Titre<input type="text" name="Titre[]" value="<?php if (!empty($titre10)) {echo $titre10;}?>"/><br/>
Photo 12 :<br/>
<?php 
if (!empty($photo11)){ echo"<img src=\"photos/mini/$photo11\" alt=\"$titre11>\"/>";}?><input type="file" name="NewPhoto[]" />
Titre<input type="text" name="Titre[]" value="<?php if (!empty($titre11)) {echo $titre11;}?>"/><br/>
Photo 13 :<br/>
<?php 
if (!empty($photo12)){ echo"<img src=\"photos/mini/$photo12\" alt=\"$titre12>\"/>";}?><input type="file" name="NewPhoto[]" />
Titre<input type="text" name="Titre[]" value="<?php if (!empty($titre12)) {echo $titre12;}?>"/><br/>
Photo 14 :<br/>
<?php 
if (!empty($photo13)){ echo"<img src=\"photos/mini/$photo13\" alt=\"$titre13>\"/>";}?><input type="file" name="NewPhoto[]" />
Titre<input type="text" name="Titre[]" value="<?php if (!empty($titre13)) {echo $titre13;}?>"/><br/>
Photo 15 :<br/>
<?php 
if (!empty($photo14)){ echo"<img src=\"photos/mini/$photo14\" alt=\"$titre14>\"/>";}?><input type="file" name="NewPhoto[]" />
Titre<input type="text" name="Titre[]" value="<?php if (!empty($titre14)) {echo $titre14;}?>"/><br/>
Photo 16 :<br/>
<?php 
if (!empty($photo15)){ echo"<img src=\"photos/mini/$photo15\" alt=\"$titre15>\"/>";}?><input type="file" name="NewPhoto[]" />
Titre<input type="text" name="Titre[]" value="<?php if (!empty($titre15)) {echo $titre15;}?>"/><br/>
Photo 17 :<br/>
<?php 
if (!empty($photo16)){ echo"<img src=\"photos/mini/$photo16\" alt=\"$titre16>\"/>";}?><input type="file" name="NewPhoto[]" />
Titre<input type="text" name="Titre[]" value="<?php if (!empty($titre16)) {echo $titre16;}?>"/><br/>
Photo 18 :<br/>
<?php 
if (!empty($photo17)){ echo"<img src=\"photos/mini/$photo17\" alt=\"$titre17>\"/>";}?><input type="file" name="NewPhoto[]" />
Titre<input type="text" name="Titre[]" value="<?php if (!empty($titre17)) {echo $titre17;}?>"/><br/>
Photo 19 :<br/>
<?php 
if (!empty($photo18)){ echo"<img src=\"photos/mini/$photo18\" alt=\"$titre18>\"/>";}?><input type="file" name="NewPhoto[]" />
Titre<input type="text" name="Titre[]" value="<?php if (!empty($titre18)) {echo $titre18;}?>"/><br/>
Photo 20 :<br/>
<?php 
if (!empty($photo19)){ echo"<img src=\"photos/mini/$photo19\" alt=\"$titre19>\"/>";}?><input type="file" name="NewPhoto[]" />
Titre<input type="text" name="Titre[]" value="<?php if (!empty($titre19)) {echo $titre19;}?>"/><br/>
<p style="text-align:center;">
 <input type="submit" class="submit" name="submit" value="Modifier"/><br />
 </p>
</form>
</article>
</section>
</body>
</html>