Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

could not display an upload image in the submit form

Status
Not open for further replies.

dldl

Programmer
May 3, 2010
40
NZ
Hi;

i am writting a form to let people submit
their information and upload an image.

My idea is if the user fill all the filed(name and age of the field) and upload an image, then i will submit the form.
if the user does not fill any of the field of the name and age, or do not upload an image, then the user fail to submit.
In this case, the user will refill the form again, and in the refilled in form, it will display the resized image that it have just uploaded and value the user just
filled.

My problem is i could not display the resized image i just upload in the filled form, if i failed to submit the form
(for example, i did not fill some filed i must fill it)


Could anyone help me,plase.

////////////////////////

//To let user submit a form and upload a image

form.php

<?php
session_start();
?>
<html>
<body>

<form action="confirmation.php" method="post" enctype="multipart/form-data">
<div>

<?php
if( $_SESSION['flag']==0){
if(!empty($_SESSION['valid_message_name']))
echo $_SESSION['valid_message_name'];
}

?>
**Name: <input type="text" name="name" value="<?php echo $_SESSION['name']; ?>"/><br>

<?php
if( $_SESSION['flag']==0){
if(!empty($_SESSION['valid_message_age']))
echo $_SESSION['valid_message_age'];
}

?>
**Age: <input type="text" name="age" value="<?php echo $_SESSION['age']; ?>"/>
<br>

add picture here

<br>
<?php

if( $_SESSION['flag']==0){
if(!empty($_SESSION['valid_message_file'])){
echo $_SESSION['valid_message_file'];
}
else{

echo "The file have uploaded already<br>";
$image=$_SESSION['image'];
echo "<img src=\"displayresizeimage.php?image=$image\" alt=\"image\" >";
}
}


?>
**<input type="file" name="myfile" id="file">


<input type="submit" name="submit" value="submit">
</form>

</body>
</html>
/////////////////////
/////////////////////

//to check whether the user fill all the filed and upload an valid image
//otherwise it will take user back to the filled in form again

confirmation.php

<?php
session_start();
//1 means the file is valid format and valid size and user fill the field
$_SESSION['flag']=1;

$_SESSION['valid_message_file']=""; //empty means the file is valid
$_SESSION['valid_message_name']=""; //empty means the name field is valid
$_SESSION['valid_message_age']=""; //empty means the age field is valid

$_SESSION['name']=""; //there is not value in the name file
$_SESSION['age']=""; //there is not value in the age file



if(empty($_POST['name'])){
$_SESSION['flag']=0;
$_SESSION['valid_message_name']="You must fill the name field";
}else{
$_SESSION['name']=$_POST['name'];

}

if(empty($_POST['age'])){
$_SESSION['flag']=0;
$_SESSION['valid_message_age']="You must fill the age field";
}else{
$_SESSION['age']=$_POST['age'];

}


if ((($_FILES["myfile"]["type"] == "image/gif")
|| ($_FILES["myfile"]["type"] == "image/jpeg")
|| ($_FILES["myfile"]["type"] == "image/pjpeg"))
&& ($_FILES["myfile"]["size"] < 100000))
{
if ($_FILES["myfile"]["error"] > 0)
{
$_SESSION['valid_message_file'] ="There are error in the file";
$_SESSION['flag']=0;

}
else
{
$_SESSION['image']=$_FILES["myfile"]["tmp_name"]."/" . $_FILES["myfile"]["name"];


}
}
else
{
$_SESSION['valid_message_file'] ="The size of the file or the format of the file is not right or you have not upload a file yet";
$_SESSION['flag']=0;

}



$flag=$_SESSION['flag'];
if($flag==1)
header('Location: //insert data and image to database
else
header('Location: //resubmit form again

?>

//////////////////////

////////////////////////////////////////////
//dipslay a resized image

displayresizeimage.php

<?php


function resizeImage($originalImage,$toWidth,$toHeight, $maintain=true){
global $imageResized,$imageTmp;
//check that image exists
if (!is_file($originalImage) || !is_readable($originalImage)) return false;
// Get the original geometry and calculate scales
list($width, $height) = getimagesize($originalImage);

if ($maintain) list($newWidth, $newHeight) = ( (($toWidth/$toHeight) > ($width/$height)) ?array (intval(($toWidth * $width/$height)), intval($toHeight)) : array (intval($toWidth), intval(($toHeight * $height/$width))));

$imagepath=$_GET['image'];

// Resize the original image
$imageResized = imagecreatetruecolor($newWidth, $newHeight);
if(exif_imagetype($imagepath)==IMAGETYPE_JPEG)
$imageTmp = imagecreatefromjpeg ($originalImage);
else if(exif_imagetype($imagepath)==IMAGETYPE_GIF)
$imageTmp = imagecreatefromgif($originalImage);
imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
return $imageResized;
}

$imageResized="";
$imageTmp="";

$imagepath=$_GET['image'];


$image=resizeImage($imagepath,123,120,"true"); //this one shows on the browser

if(exif_imagetype($imagepath)==IMAGETYPE_JPEG)
header('Content-type:image/jpeg');
else if(exif_imagetype($imagepath)==IMAGETYPE_GIF)
header('Content-type:image/gif');
if(exif_imagetype($imagepath)==IMAGETYPE_JPEG)
imagejpeg($image, null, 20);
else if(exif_imagetype($imagepath)==IMAGETYPE_GIF)
imagegif($image, null, 20);


imagedestory($imageResized);
imagedestory($imageTmp);



?>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top