IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Langage PHP Discussion :

Prévisualiser une image


Sujet :

Langage PHP

  1. #21
    apt
    apt est déconnecté
    Membre éclairé
    Inscrit en
    Mai 2002
    Messages
    867
    Détails du profil
    Informations forums :
    Inscription : Mai 2002
    Messages : 867
    Par défaut
    Bon ce que j'ai fait :

    l'utilisateur prévisualise ce qu'il veut (les images sont dans le dossier tempo).

    Une fois qu'il décide de valider son formulaire (IsSet($_POST['add']), tout ce qui se trouve dans tempo est supprimé (parce que l'image valide à été chargé dans le dossier uploads).

    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
    if (IsSet($_POST['add']))
     {
     
      $contenu = array(); 
      $ressources = dir("tempo/"); 
        while($lecture = $ressources->read()){ 
            if($lecture != "." && $lecture != ".."){ 
                $contenu[] = $lecture; 
            } 
        } 
        $ressources->close(); 
     
        foreach($contenu as $v){ 
          @unlink("tempo/".$v); 
        } 
    ...
    }

  2. #22
    apt
    apt est déconnecté
    Membre éclairé
    Inscrit en
    Mai 2002
    Messages
    867
    Détails du profil
    Informations forums :
    Inscription : Mai 2002
    Messages : 867
    Par défaut
    Un nouveau code.

    S'il y'a des erreurs me les signaler.

    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
    <?php
    define("MAX_SIZE",200000); 
    function verifiesimage($link, $limit) {
        if( preg_match('#^http:\/\/(.*)\.(gif|png|jpg|jpeg)$#i', $link) && strlen($link) < $limit )
        {
            $msg = TRUE; // link ok
        }
        else
        {
            $msg = FALSE; // the link isn't image
        }
        return $msg; // return TRUE or FALSE
    }
    ?>
    <!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>
      <title>News (add - preview)</title>
      <meta http-equiv="content-type" content="text/html; charset=utf-8" />
     
    </head>
     
    <body>
    <?php
     
     
    if (IsSet($_POST)) {
     
        $title = $_POST['title'];
        $post = $_POST['post'];
        $titleimg = $_POST['titleimg'];
    	echo strlen($_POST['linkupload'])."<br />";
    	if(verifiesimage($_POST['linkupload'], 100) == TRUE)
    	{
    		$linkupload = $_POST['linkupload'];
    		echo $msg."<br />";
     
    	}
    	$img_ext = array('png','jpg','jpeg','gif');
        if (isset($_FILES['fileupload'])) {
          if (is_uploaded_file($_FILES['fileuploaad']['tmp_name'])) {
    		 if (!in_aray($_FILES['fileuploaad']['type'],$img_ext)) {
                echo "<p>image upload must be uploaded in (png,jpg,jpeg,gif) format.</p>";
             } else {
    			$target_path1 = "uploads/" . basename( $_FILES['fileupload']['name']);
    		 } 
          }
       }
     
    }
     
    if (IsSet($_POST['add']))
     {	
    	// erase the tempo files
    	$extension = array('html','php');
    	$files = array();
    	$contenu = array();
    	$ressources = dir("tempo/");
     
        while($lecture = $ressources->read()){
            if($lecture != "." && $lecture != ".."){
                if(preg_match('/(index|news)/i',$lecture)){
                    array_push($files,$lecture);
                }
                $contenu[] = $lecture;
            }
        }
        $ressources->close();
     
        foreach($contenu as $v){
            $ext = pathinfo("tempo/".$v,PATHINFO_EXTENSION);
     
    		if(!in_array($ext,$extension) && !in_array($v,$files)){
                @unlink("tempo/".$v);
    		}
        } 
     
        if(move_uploaded_file($_FILES['fileupload']['tmp_name'], $target_path1)) {
             $errimg = "The file ".  basename( $_FILES['fileupload']['name'])." has been uploaded";
        } else{
        $errimg .= "There was an error uploading the file, please try again!";
        }
     
        echo $errimg;
     
     
    	$url = $linkupload;
    	// the path of save
    	$path = 'uploads/';
     
    	$exp = explode('/',$url);
    	$serv = $exp[0].'//'.$exp[2];
    	// the name of file
    	$filename = array_pop($exp); 
    	//check the hack namefile
    	if( preg_match('#[\x00-\x1F\x7F-\x9F/\\\\]#', $filename) )
    	{
    		exit("Invalid name of file");
    	}
    	//check the size of filename
    	if(filesize($filename) > MAX_SIZE){
    		echo "\nThe size of the file is great than ".MAX_SIZE;
    		exit(1);
    	}
    	$xcontext = stream_context_create(array("http"=>array("header"=>"Referer: ".$serv."\r\n")));
    	$content = file_get_contents($url,false,$xcontext);
    	if ($content === false) {
    		echo "\nImpossible to get file.";
    		exit(1);
    	}
     
    	$test = file_put_contents($path.'/'.$filename,$content);
    	if ($test === false) {
    		echo "\nImpossible to save the file.";
    		exit(1);
    	}
    	// All it's OK
    	echo "\nSaving file successfull.";
    	$target_path = $path.'/'.$filename;
     
        // ===== do the insert here
        //$sql = 'INSERT INTO your_table ...';
        //echo '<meta http-equiv="refresh" content="2; url=addnews.php" />';
        //header("Location: addnews.php");
    }
    elseif (IsSet($_POST['prev'])) {
     
    	$target_temp_path = "tempo/";
        $target_temp_path1 = $target_temp_path . basename( $_FILES['fileupload']['name']);
     
        if(move_uploaded_file($_FILES['fileupload']['tmp_name'], $target_temp_path1)) {
    		$errimg = "The file ".  basename( $_FILES['fileupload']['name'])." has been uploaded\n";
        } else{
    		$errimg .= "There was an error uploading the file ".basename( $_FILES['fileupload']['name']).", please try again!\n";
        }
     ?>
    <div style="display:block;">
        <p><b>Preview news</b></p>
        <p><?=$title;?></p>
        <div>
        <table style="flot:left; border : 1px solid #eee;">
            <tbody>
                <tr height="1"></tr>
                <tr>
                    <td width="1"></td>
                    <td align="center">
                        <img height="200" width="225" align="left" src="<?=$target_temp_path1;?>" alt="">
                        <img height="200" width="225" align="left" src="<?=$linkupload;?>" alt="">
                    </td>
     
                </tr>
                <tr>
                    <td width="1"></td>
                    <td align="center"><font size="2" color="#333300"><?=$titleimg;?></font></td>
                    <td width="1"></td>
                    <td></td>
                </tr>
             </tbody>
        </table>
        </div>
        <span><?=$post;?></span>
        <br />
        <hr color="#0000FF" />
    </div>
    <?php
    }
    ?>
     
    <div style="display:block;">
    <form enctype="multipart/form-data" method="post" action="<?=$_SERVER['PHP_SELF']?>">
     
        <p>News title :
        <input type="text" name="title" value="<?=$title;?>" size="40" /></p>
        <p>Post :
        <textarea name="post" rows="10" COLS="40" ><?=$post?></textarea></p>
        <p>Image From PC :
        <input type="file" name="fileupload" /></p>
        <p>Image from link :
        <input type="text" name="linkupload" value="<?=$linkupload;?>"/></p>
        <p>Title of image :
        <input type="text" name="titleimg" value="<?=$titleimg;?>"/></p>
        <p>Title of image :
        <input name="add" type="submit" value="Insert" />
        <input name="prev" type="submit" value="Preview" /></p>
     
    </form>
    </div>
    </body>
    </html>
    Merci.

+ Répondre à la discussion
Cette discussion est résolue.
Page 2 sur 2 PremièrePremière 12

Discussions similaires

  1. [AJAX] Prévisualiser une image avant le téléchargement
    Par zied.ellouze dans le forum AJAX
    Réponses: 0
    Dernier message: 19/10/2011, 13h04
  2. Prévisualisation d'une image
    Par Stefounette dans le forum JavaFX
    Réponses: 1
    Dernier message: 06/04/2010, 17h00
  3. Prévisualisation d'une image sélectionnée sur disque dur
    Par ero-sennin dans le forum Général JavaScript
    Réponses: 12
    Dernier message: 03/08/2008, 00h54
  4. Prévisualisation d'une image
    Par chris28200 dans le forum Général JavaScript
    Réponses: 3
    Dernier message: 08/08/2007, 17h01
  5. prévisualiser une image avant upload
    Par johan_b dans le forum Général JavaScript
    Réponses: 4
    Dernier message: 26/01/2007, 21h42

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo