View Single Post
  #1 (permalink)  
Old Aug 1st, 2006, 23:04
AdRock's Avatar
AdRock AdRock is offline
SuperMember

SuperMember
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
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