8796 sujets

Développement web côté serveur, CMS

Bonjour,

J'aimerais pouvoir prévisualiser une image chargé depuis mon pc avant l'envoie vers la bdd.

mais je n'arrive toujours pas.

<!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 ($_POST) {
    @extract($_POST);
}

if (IsSet($_POST['add']))
 {
     $target_path = "uploads/";
     $target_path1 = $target_path . basename( $_FILES['fileupload']['name']);
     $target_path2 = $target_path . basename( $_FILES['imgupload']['name']);

     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!";
    }

    if(move_uploaded_file($_FILES['imgupload']['tmp_name'], $target_path2)) {
         $errimg .= "The file ".  basename( $_FILES['imgupload']['name'])." has been uploaded";
    } else{
    $errimg .= "There was an error uploading the file, please try again!";
    }
    echo $errimg;

     // 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'])) {
?>
<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="<?=$_FILES['fileupload']['tmp_name'];?>" alt="">
                    <img height="200" width="225" align="left" src="<?=$_FILES['linkupload']['tmp_name'];?>" 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="Inzert" />
    <input name="prev" type="submit" value="Preview" /></p>

</form>
</div>
</body>
</html>


http://majallati.comli.com/test/preview/addnews.php

Merci d'avance.
Modifié par apt (27 Mar 2011 - 15:14)
Voila à nouveau le code.

mais il y' un problème : les images ne sont pas uploader dans le dossier tempo mais dans le même emplacement que le fichier appelant qui est addnews.php.

ou est l'erreur ?

<!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 ($_POST) {
   
    $title = $_POST['title'];
    $post = $_POST['post'];
    $titleimg = $_POST['titleimg'];
	$linkupload = $_POST['linkupload'];
}
 
if (IsSet($_POST['add']))
 {
     $target_path = "uploads/";
     $target_path1 = $target_path . basename( $_FILES['fileupload']['name']);
     
 
     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); 
// 
$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_temp_path2 = $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_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
unset($target_temp_path1);


}
?>
 
<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>