| Welcome to Webforumz.com. |
|
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
|
|
|
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
|
|
|
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
|
|
|
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
|
|
|
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!
|
|
|
| Thread Tools |
|
|
| 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
HTML code is Off
|
|
|
|
|
|