Web Design and Development Forums

upload images through updating a form

This is a discussion on "upload images through updating a form" within the PHP Forum section. This forum, and the thread "upload images through updating a form are both part of the Program Your Website category.


Go Back   Webforumz.com > Program Your Website > PHP Forum

Welcome to Webforumz.com.
Register Now Register now!

Reply
 
LinkBack Thread Tools Rate Thread
Old Apr 13th, 2008, 15:00   #1 (permalink)
Junior Member
 
Join Date: Aug 2007
Location: London
Posts: 13
upload images through updating a form

Dear all,

I have created a form to edit existing data in my database. It works fine, however I need to update an image. The updating of the image name in the database works fine, but I need to write the image onto the server into an image folder so that it displays it on the webpage, but this isn’t working. Could anyone check my code and let me know what I should do?
Thanks
Mark

Code: Select all
   
  <form method="post" action="updated.php">
  <table>
  <col span="1" align="right">
  <tr>
  <td><?php Echo "<img src=http://www.domainname.com/images/".$formVars['photo'] ."> "; ?></td>
  </tr>
  <tr>
  <td>id:</td>
  <td><? echo $formVars["id"]; ?></td>
  </tr>
  <tr>
  <td>Name:</td>
  <td><input type="text" name="name"
  value="<? echo $formVars["name"]; ?>" size=40 maxlength=30></td>
  </tr>
  <tr>
  <td>Photo:</td>
  <td><input type="file" name="photo" 
  value="<? echo $formVars["photo"]; ?>" size=40 maxlength=30></td>
  </tr>
  <form enctype="multipart/form-data" action="updated.php" method="POST">
  <input name="id" type="hidden" value="<?=$formVars[id]?>" />
  <input name="submit" type="submit" value="Submit" /></form></td>
  </tr>

Last edited by saltedm8; Apr 17th, 2008 at 17:29. Reason: added [code] tags
markusdavid is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 13th, 2008, 16:37   #2 (permalink)
Up'n'Coming Member
 
Join Date: Sep 2006
Location: UK
Posts: 65
Re: upload images through updating a form

http://uk2.php.net/features.file-upload
balaclave is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 14th, 2008, 19:42   #3 (permalink)
Reputable Member
 
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 294
Re: upload images through updating a form

You seem to have two open form tags and one close form tag - so the behaviour may vary depending on the browser. And I suspect that most of the "issues" you have will be in update.php rather than in your code above.

Post up you code from update.php and I (or someone) will have a look. I can also give you a link to a page that uploads and stores an image - here - but in that example I keep the image in the database too. Much cleaner
__________________
Graham Ellis ...
Open Source trainer with Well House Consultants
grahame is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old Apr 15th, 2008, 21:15   #4 (permalink)
Junior Member
 
Join Date: Aug 2007
Location: London
Posts: 13
Re: upload images through updating a form

Hiya and thanks for the replies.
The open and closed form tags are from copying and pasting the code here, as this is just a snippet of the complete code.

I tried to use this code in the update.php file, but it didn’t work.

PHP: Select all

//This is the directory where images will be saved 
  
$target "http://www.webforumz.com/images/"
  
$target $target basename$_FILES['photo']['name']);
   
  
QUERY (from the update.php code below)
   
  
//Writes the photo to the server 
  
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
  { 
   
  
//Tells you if its all ok 
  
echo "The file "basename$_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory <br><a href=\"view.php\">click here</a> to update another record<br>"
  } 
  else { 
   
  
//Gives and error if its not 
  
echo "Sorry, there was a problem uploading your file <br><a href=\"addpropertyform.php\">click here</a> to add another Property<br>"
  } 

Below is the update.php code

PHP: Select all

<?php
   
  
foreach($HTTP_POST_VARS as $varname => $value)
  
$formVars[$varname]=$value;
   
  
mysql_connect("host""table""password"); 
  
mysql_select_db("database"); 
   
  echo 
"Record updated<br><a href=\"view.php\">click here</a> to update another record<br>";
   
  
$query="UPDATE property SET ".
  
"name= \"".$formVars["name"]."\",".
  
"photo= \"".$formVars["photo"]."\",".
  
"desc6= \"".$formVars["desc6"].
  
"\" WHERE id = \"".$formVars["id"]."\"";
  
mysql_query($query);
   
  
?>
Thanks for all the answers and help!!!

Last edited by saltedm8; Apr 17th, 2008 at 17:30. Reason: added [php] tags
markusdavid is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Old May 8th, 2008, 17:17   #5 (permalink)
Reputable Member
 
Join Date: Oct 2007
Location: Liverpool
Age: 29
Posts: 193
Re: upload images through updating a form

First of you need to add this line to the top of your form:

HTML: Select all
<form action="do_update.php" method="post"enctype="multipart/form-data">
Add validatation for your file upload types like so:

PHP: Select all

$img 'image/';
//accepted image types
$accepted = array(
             
$img.'png',
             
$img.'jpg'
             
$img.'jpeg'
             
$img.'gif'
             
$img.'pjpeg'
             
$img.'x-png'
             
$img.'pjpg'
                 
);
$maxSize 100000
Encapsulate the post data from your form:

PHP: Select all

$id $_SESSION['ID'];
$name $_POST['Name'];
$surname $_POST['Surname'];
$address $_POST['Address'];
bla bla 
Lastly work with the photo:


PHP: Select all

//work with the photo
$worryAboutPhoto = !empty($_FILES['photo']['name']); 

if (
$worryAboutPhoto)
{
    
$uploaded false;
    
$photo $_FILES['photo'];
    
$trash explode('.'$photo['name']);
    
$ext $trash[sizeof($trash) - 1];
    
$location 'file_uploads/'.$id.'.'.$ext;
    if (
$photo['size'] > $maxSize)
    {
        
flash_warning('The filesize is too large (max of '.$maxSize.' bytes)');
    }
    else if (!
in_array($photo['type'], $accepted))
    {
        
flash_warning('File type not accepted');
    }
    else
    {
        if (!
move_uploaded_file($photo['tmp_name'], $location))
        {
            
flash_warning('Error uploading file. Please try again');
        }
        else
        {
            
$uploaded true;
        }
    }
}
else 
$uploaded true;

//create the $params array
$newVals = array(
    
'detailsID' => $id,
    
'name' => $name,
    
'surname' => $surname,
    
'address' => $address,
    
'email' => $email,
    
'phone' => $phone,
    
'username' => $username,
    
'password' => $password
    
);
    
//see update_fns.php
if ($uploaded)
{
    if (
updateAcct($newVals))
    {
        
header('Location: accounts.php');
    }
}
else
{
    echo 
'<p>One or more errors occurred</p>';

Make sure you create & include you upload functions!

Good luck mate! Its easy once you get the hang of it!
longstand is offline  
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
upload images in database and folder manzil PHP Forum 1 Aug 11th, 2006 16:06
how to upload images to the server? maksinx HTML Forum 1 Jul 27th, 2006 21:39
Updating images - how to do it, please? acegap Introduce Yourself 9 Apr 24th, 2006 22:25
form not updating values djanim8 ASP Forum 0 Dec 10th, 2005 19:05
Looking for an asp form to upload images chuckcampbell ASP Forum 2 Oct 8th, 2005 21:36



Latest Updates

All Points SEO Security Advisory - CHECK YOUR SITE NOW!

Creative Coding :: February 2008

Webforumz is sponsored by: WESH UK Web Hosting
All times are GMT. The time now is 19:15.

Sleep Study Scoring :: Free Bet :: Website Templates :: Online Betting :: Bookmakers :: Funny Quotes :: Internet Recruitment Software :: Microsoft CRM Experts :: Online Casino :: Decorated Christmas Trees :: Midwife Forums :: Football Betting :: Ecommerce Software :: Web Hosting :: Football Stats :: Dry Cleaning Collection :: xtreme wales - extreme clothing :: Apuestas :: Sharepoint Consultants :: Website Optimisation :: Office Clearance London :: Sharepoint Experts :: Sports Betting :: Casino :: Website Templates :: Web Design Development India :: Online Gambling

Powered by: vBulletin Version 3.7, Copyright ©2000 - 2008, Jelsoft Enterprises Limited.
© 2003-2008 Webforumz.com : All Rights Reserved
Search Engine Friendly URLs by vBSEO 3.2.0 RC6


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