Current location: Hot Scripts Forums » Programming Languages » PHP » insert image in html using php(variable)

insert image in html using php(variable)

Reply
  #1 (permalink)  
Old
Newbie Coder
 
Join Date: Oct 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
insert image in html using php(variable)

Hi

I am creating a web page which allows visitors to upload images. I am then resizing and renaming the images. I would like to have the visitor validate the resized image and added info before proceeding (giving them chance to resubmit).

How can I insert the image into the correct size cell in a dynamic web page using a php variable. (I am renaming to a standard format relating to the name of the uploader John_Smith01.jpg etc. This name is created using the PHP variables.

As all this is done at run time I do not know the name of the files beforehand, therefore cannot use the <img src "path name"> as path name is a php variable

eg. $folder.$name

I have tried variations of
<img src=<?php$folder.$name?></img>;

Many Thanks in advance
Dave
Reply With Quote

Featured Marketplace Scripts (view more)

  #2 (permalink)  
Old
wirehopper's Avatar
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 110 Times in 107 Posts
Options:

Use the session id to name the file - "<?php echo '/tmp/'.session_id().'.jpg'; ?>"

Use 'John_Smith_TEMP.jpg' until the user validates it.

Then, rename the file after the user validates it.
Reply With Quote
  #3 (permalink)  
Old
job0107's Avatar
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 141 Times in 138 Posts
Post your existing code so we can see what you are doing.
Then we can help you get it working.
__________________
Jerry Broughton
Reply With Quote
  #4 (permalink)  
Old
Newbie Coder
 
Join Date: Oct 2008
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
The code for renaming the uploaded file is :
PHP Code:

<?php
$name 
basename($_FILES['the_file']['name']);
$tmp_name $_FILES['the_file']['tmp_name'];
$size $_FILES['the_file']['size'];
$folder $_SERVER['DOCUMENT_ROOT']."/Cards/Images/";

$fname $_POST["GK1_f_name"];
$lname $_POST["GK1_l_name"];

if (
$size <= 1048576) {
    
// check if the upload has happened then rename it in the correct location
    
$name2 "GK1_".$fname."_".$lname.".jpg";
     
rename($tmp_name,$folder.$name2);
    } else {
        echo 
'There was an error with the upload.';
    }
?>

I am then trying to display the image in an html page(I will resize it once this works!)

?>
<html>
<head>
<title>Image Validation</title>
</head>

<body>
<p>
Please check the image, name and position are correct before validating </p>
<p>Please Note !!!</p>
<br>
The image and information you validate will be used
<br>
<table><tr><td>
<?php
$image 
$folder.$name;
<
img src=$image></img>;
?>
</td></tr></table>
</body>
</html>
I am new to this so any help woill be gratefully recieved.

Last edited by Nico; 11-12-08 at 11:08 AM. Reason: Wrappers.
Reply With Quote
  #5 (permalink)  
Old
bizzar528's Avatar
Community Liaison
 
Join Date: Sep 2004
Location: Pennsylvania, US
Posts: 1,549
Thanks: 2
Thanked 16 Times in 15 Posts
Instead of using the rename function, use move_uploaded_file.

http://us.php.net/move_uploaded_file

PHP Code:

move_uploaded_file($tmp_name,$folder.$name2); 

As wirehopper said though, you can call the image using it's temp name as well for the validation part before moving it.

Last edited by bizzar528; 11-12-08 at 03:36 PM.
Reply With Quote
  #6 (permalink)  
Old
job0107's Avatar
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 141 Times in 138 Posts
You might try something like this:

PHP Code:

<?php
if(!empty($_POST["cancel"])){unlink($_POST["theFile"]);}
if(!empty(
$_POST["confirm"]))
{
 
// Do something after the application has been accepted
 
echo "Your application has been submitted.";
 }
else
{
 if(!empty(
$_POST["submit"]))
 {
  
$msg="";
  
$folder=$_SERVER['DOCUMENT_ROOT']."/Cards/Images/";
  
$fname=empty($_POST["GK1_f_name"])?"":$_POST["GK1_f_name"];
  
$lname=empty($_POST["GK1_l_name"])?"":$_POST["GK1_l_name"];
  if(!empty(
$_FILES))
  {
   
$name=basename($_FILES['the_file']['name']);
   
$tmp_name=$_FILES['the_file']['tmp_name'];
   
$size=$_FILES['the_file']['size'];
   }
  if(
$fname&&$lname&&$size&&$size<=1048576)
  {
   
$name="GK1_".$fname."_".$lname."_".$name;
   if(!
file_exists($folder.$name))
   {
    if(!
move_uploaded_file($tmp_name,$folder.$name)){echo 'There was an error with the upload.';}
    }
   }
  else
  {
   if(!
$fname){$msg.="You must enter your first name.<br />";}
   if(!
$lname){$msg.="You must enter your last name.<br />";}
   if(empty(
$_FILES['the_file']['name'])){$msg.="You did not select a file to upload.<br />";}
   }
  }
?>
<html>
<head>
<title>Image Validation</title>
</head>
<body>
<table><tr><td>
<?php
function confirm_upload()
{
 global 
$folder,$name,$fname,$lname;
 
$confirm_form="<form action='#' method='post'>
                 <div>Firsf name: 
$fname</div>
                 <div>Last name: 
$lname</div>
                 <br />
                 <input type='submit' name='confirm' value='Accept'>
                 <input type='submit' name='cancel' value='Cancel'>
                 <input type='hidden' name='theFile' value='"
.$folder.$name."'>
                </form>"
;
 return 
"<img src='".$folder.$name."' width='150px' height='150px' /><p>".$confirm_form;
 }
if(
$name){echo confirm_upload();}
else
{
 if(
$msg){echo "<div style='border:3px solid #f00;padding:10px;'>$msg</div><br />";}
 echo 
"<img src='' width='150px' height='150px' />";
?>
</td></tr></table>
<form method="post" action="#" ENCTYPE="multipart/form-data">
 <input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
 <table>
  <tr>
   <td>First Name: </td>
   <td><input type="text" name="GK1_f_name" value="<?php echo $fname?>"></td>
  </tr>
  <tr>
   <td>Last Name: </td>
   <td><input type="text" name="GK1_l_name" value="<?php echo $lname?>"></td>
  </tr>
  <tr>
   <td>Select file: </td>
   <td><input type="file" name="the_file" /></td>
  </tr>
  <tr>
   <td colspan="2" align="center" style="padding-top:10px;"><input type="submit" name="submit" value="Submit Application" /></td>
  </tr>
 </table>
</form>
</body>
</html>
<?php }} ?>
__________________
Jerry Broughton

Last edited by job0107; 11-13-08 at 04:15 PM.
Reply With Quote
  #7 (permalink)  
Old
job0107's Avatar
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 141 Times in 138 Posts
Actually, that first program worked OK,
but it wasn't quite right.
It had a bug, when you selected an image file without entering your first and last name.

This code works much better:

PHP Code:

<?php
if(!empty($_POST["cancel"])){unlink($_POST["theFile"]);}
if(!empty(
$_POST["confirm"]))
{
 
// Do something after the application has been accepted
 
echo "Your application has been submitted.";
 }
else
{
 if(!empty(
$_POST["submit"]))
 {
  
$msg="";
  
$folder=$_SERVER['DOCUMENT_ROOT']."/Cards/Images/";
  
$fname=empty($_POST["GK1_f_name"])?"":$_POST["GK1_f_name"];
  
$lname=empty($_POST["GK1_l_name"])?"":$_POST["GK1_l_name"];
  if(
$fname&&$lname)
  {
   if(!empty(
$_FILES['the_file']['name']))
   {
    
$name=basename($_FILES['the_file']['name']);
    
$tmp_name=$_FILES['the_file']['tmp_name'];
    
$size=$_FILES['the_file']['size'];
    }
   }
  if(
$size&&$size<=1048576)
  {
   
$name="GK1_".$fname."_".$lname."_".$name;
   if(!
file_exists($folder.$name))
   {
    if(!
move_uploaded_file($tmp_name,$folder.$name)){echo 'There was an error with the upload.';}
    }
   }
  else
  {
   if(!
$fname){$msg.="You must enter your first name.<br />";}
   if(!
$lname){$msg.="You must enter your last name.<br />";}
   if(empty(
$_FILES['the_file']['name'])||(!$fname&&!$lname)){$msg.="You did not select a file to upload.<br />";}
   }
  }
?>
<html>
<head>
<title>Image Validation</title>
</head>

<body>
<table><tr><td>
<?php
function confirm_upload()
{
 global 
$folder,$name,$fname,$lname;
 
$confirm_form="<form action='#' method='post'>
                 <div>First name: 
$fname</div>
                 <div>Last name: 
$lname</div>
                 <br />
                 <input type='submit' name='confirm' value='Accept'>
                 <input type='submit' name='cancel' value='Cancel'>
                 <input type='hidden' name='theFile' value='"
.$folder.$name."'>
                </form>"
;
 return 
"<img src='".$folder.$name."' width='150px' height='150px' /><p>".$confirm_form;
 }
if(
$name){echo confirm_upload();}
else
{
 if(
$msg){echo "<div style='border:3px solid #f00;padding:10px;'>$msg</div><br />";}
 echo 
"<img src='' width='150px' height='150px' />";
?>
</td></tr></table>
<form method="post" action="#" ENCTYPE="multipart/form-data">
 <input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
 <table>
  <tr>
   <td>First Name: </td>
   <td><input type="text" name="GK1_f_name" value="<?php echo $fname?>"></td>
  </tr>
  <tr>
   <td>Last Name: </td>
   <td><input type="text" name="GK1_l_name" value="<?php echo $lname?>"></td>
  </tr>
  <tr>
   <td>Select file: </td>
   <td><input type="file" name="the_file" /></td>
  </tr>
  <tr>
   <td colspan="2" align="center" style="padding-top:10px;"><input type="submit" name="submit" value="Submit Application" /></td>
  </tr>
 </table>
</form>
</body>
</html>
<?php }} ?>
__________________
Jerry Broughton
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Auto Dealer Script nuzzle PHP 17 04-14-10 09:34 PM
div css theighost CSS 11 09-14-08 03:30 AM
displaying text over image ketanco CSS 10 09-11-08 08:39 PM
Need a simple script to insert HTML Danica JavaScript 1 05-28-05 11:56 AM
Please help insert html into php idforforums PHP 5 04-07-05 12:29 AM


All times are GMT -5. The time now is 05:42 PM.
vBulletin® Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.