Another question about uploading images using form

This is a discussion on "Another question about uploading images using form" within the PHP Forum section. This forum, and the thread "Another question about uploading images using form are both part of the Program Your Website category.



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

Notices


Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old Aug 1st, 2006, 23:04
AdRock's Avatar
SuperMember

SuperMember
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to AdRock
Another question about uploading images using form

I am having trouble with uploading my image to an images folder on the server using my form.

My form currently checks for empty fields and if they are empty it displays an error message but I want to upload the image file to the remote directory (images/) but at the same time save the file-name in the table.

This would make it easier to match records with their image for display purposes

Here is the code that I use
PHP: Select all

<?php
/**
* Change the email address to your own.
*
* $empty_fields_message and $thankyou_message can be changed
* if you wish.
*/
// This is displayed if all the fields are not filled in
$empty_fields_message "<p>Please go back and complete all the fields in the form.</p>Click <a class=\"two\" href=\"javascript:history.go(-1)\">here</a> to go back";
// This is displayed when the email has been sent
$thankyou_message "<p class=\"style3\">Thankyou. News successfully added.</p><p>Click <a class=\"two\" href=\"index.php\">here</a> to go back to Jack Godfrey Honeylands Support Fund Admin Area main menu.</p>";
 
//This is the directory where images will be saved 
$target "images/"
$target $target basename$_FILES['photo']['name']); 
//This gets all the other information from the form
$name addslashes($_POST['txtName']);
$message addslashes($_POST['txtMessage']);
$pic=($_FILES['photo']['name']);
if (!isset(
$_POST['txtName'])) {
?>
 
<h3>Jack Godfrey Honeylands Support Fund Admin Area.</h3>
<h3 style="color:#060B69;">Add a news item</h3>
<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
<p class="style1">Please enter a title for the news item.
<input style="width:400px;" type="text" title="Please enter a title for the news item" name="txtName" size="30"/></p>
<p class="style1">Please enter the content for the news item.
<textarea style="width:400px;" title="Please enter the content for the news item" name="txtMessage" rows="10" cols="30"></textarea></p>
<p class="style1">Please select an image for the news item.
<input style="width:400px;" type="file" name="photo"></p>
<p class="style3">
<input class="submit-button" style="margin-left:0" type="Submit" value="Submit"><input class="submit-button" style="margin-left:2px" type="reset" value="Reset">
</form>
<p>Click <a class="two" href="index.php">here</a> to go back to Jack Godfrey Honeylands Support Area admin area</p>
<?php
}
elseif (empty(
$name) || empty($message)) {
echo 
$empty_fields_message;
}
else {
// Stop the form being used from an external URL
// Get the referring URL
$referer $_SERVER['HTTP_REFERER'];
// Get the URL of this page
$this_url "http://".$_SERVER['HTTP_HOST'].$_SERVER["REQUEST_URI"];
// If the referring URL and the URL of this page don't match then
// display a message and don't send the email.
if ($referer != $this_url) {
echo 
"You do not have permission to use this script from another URL.";
exit;
}
include_once(
"../includes/connection.php");
// The URLs matched so send the email
mysql_connect($host,$user,$password);
@
mysql_select_db($database) or die( "Unable to select database");
$query "INSERT INTO news VALUES ('','$name','$message',now()),'$pic'";
mysql_query($query);
//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"

else { 
//Gives and error if its not 
echo "Sorry, there was a problem uploading your file."

mysql_close();
// Display the thankyou message
echo $thankyou_message;
 
}
?>
Before I thought about adding the image to the post the script worked to insert a new record

I get the following errors:

Warning: move_uploaded_file(images/img.JPG): failed to open stream: No such file or directory in /home/adrock/public_html/jack/admin/addnews.php on line 82
Warning: move_uploaded_file(): Unable to move '/tmp/phpS56su2' to 'images/img.JPG' in /home/adrock/public_html/jack/admin/addnews.php on line 82
Sorry, there was a problem uploading your file.


I have inlcuded teh text file with the whole script in
Attached Files
File Type: txt addnews.txt (3.4 KB, 35 views)
Reply With Quote

  #2 (permalink)  
Old Aug 2nd, 2006, 03:14
Reputable Member
Join Date: Jul 2005
Location: Melksham, Wilts, UK
Posts: 293
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Another question about uploading images using form

I would suspect that the directory /home/adrock/public_html/jack/admin/images does not exist.
Reply With Quote
  #3 (permalink)  
Old Aug 2nd, 2006, 08:51
AdRock's Avatar
SuperMember

SuperMember
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to AdRock
Re: Another question about uploading images using form [FIXED]

Thanks....that was it....it was going to the wrong directory. I don't know how I overlooked that. I should have used ../images/
Reply With Quote
Reply

Tags
question, uploading, images, using, form

Thread Tools

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
Uploading clean images MonaE Graphics and 3D 29 Dec 10th, 2006 03:31
Uploading clean images MonaE Web Page Design 1 Dec 1st, 2006 21:58
Uploading Images: Help Need! NewDesigner Classic ASP 0 Oct 12th, 2006 19:08
Uploading and displaying images. robukni PHP Forum 8 Jan 18th, 2006 12:39
uploading images benbacardi Classic ASP 2 May 20th, 2005 16:15


All times are GMT. The time now is 12:03.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

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