Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old October 22nd, 2009, 06:00 AM
Authorized User
 
Join Date: Aug 2009
Posts: 12
Thanks: 1
Thanked 0 Times in 0 Posts
Default How to: Upload image to server and save image name in db with other form info

Hello all,

Could you please advise me on the following:
I have created a from to insert data into mysql database, including an uploadimage on the form. The image gets uploaded to the server and the name of image to the db, but I keep getting "Sorry, there was a problem uploading your file" message.

Here is the form and the code to process the data I have:

Code:
<html>
<head>
<title>Insert business for sale</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript" src="../js/country.js"></script>
</head>

<body>
<table width="100%" border="0" cellspacing="0" cellpadding="5" class="main">
  <tr> 
    <td colspan="3">&nbsp;</td>
  </tr>
  <tr> 
    <td width="160" valign="top"><p>&nbsp;</p>
      <p>&nbsp; </p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p></td>
    <td width="732" valign="top"><p>
	</p>
      <h3 class="titlehdr">Add details for business for sale here:</h3>
     
	  Or go back to Admin Panel <a href="admin_main.php">here</a>.<br>
      <form method="post" action="addcourse.php" enctype="multipart/form-data">
        <table width="95%" border="0" cellpadding="3" cellspacing="3" class="forms">
          <tr> 
            <td colspan="2" height="49"><font color="#FF0000">Course Name</font><font color="#CC0000">*</font><br>
              <input name="course" type="text" id="course" size="40">
            </td>
            <td width="4"></td>
          </tr>
          <tr> 
            <td colspan="2" height="21">&nbsp;</td>
            <td></td>
          </tr>
          <tr> 
            <td colspan="2" height="114"><font color="#FF0000">Contact Address 
              (with ZIP)</font><font color="#CC0000">*</font><br>
              <textarea name="address" cols="40" rows="4" id="address"></textarea>
            </td>
            <td></td>
          </tr>
          <tr> 
            <td height="21" valign="top" colspan="2"><font color="#FF0000"><b>Company 
              details:</b></font></td>
            <td></td>
          </tr>
          <tr> 
            <td height="59" valign="top" colspan="2"> 
              <textarea name="profile" cols="80" rows="8" id="profile" onkeyup="if (this.value.length > 300) { alert('Character limit has been reached!'); this.value = this.value.substr(0,5); }"></textarea>
            </td>
            <td></td>
          </tr>
          <tr> 
            <td height="21" valign="top" colspan="2">&nbsp;</td>
            <td></td>
          </tr>
          <tr> 
            <td height="32" width="125" valign="top"><font color="#FF0000">Contact 
              Person</font></td>
            <td width="482" valign="top"> 
              <input type="text" name="contact" id="contact">
            </td>
            <td></td>
          </tr>
          <tr> 
            <td height="21" colspan="2" valign="top"><font color="#FF0000" size="1">Please 
              select your country/state</font> <font color="#FF0000">*</font></td>
            <td></td>
          </tr>
          <tr> 
            <td height="27" valign="top"> 
              <select id='countrySelect' name='country' onchange='populateState()'>
              </select>
            </td>
            <td valign="top"> 
              <select id='stateSelect' name='state'>
              </select>
              <script type="text/javascript">initCountry('PLS'); </script>
            <td></td>
          </tr>
          <tr> 
            <td height="40" valign="top"><font color="#FF0000">City</font></td>
            <td valign="top"> 
              <input type="text" name="city" id="city">
            </td>
            <td></td>
          </tr>
          <tr> 
            <td height="34" valign="top"><font color="#FF0000">Phone</font><font color="#CC0000">*</font> 
            </td>
            <td valign="top"> 
              <input name="tel" type="text" id="tel">
            </td>
            <td></td>
          </tr>
          <tr> 
            <td height="34" valign="top"><font color="#FF0000">Holes</font> </td>
            <td valign="top"> 
              <input name="holes" type="text" id="holes">
            </td>
            <td></td>
          </tr>
          <tr> 
            <td height="34" valign="top"><font color="#FF0000">Par</font> </td>
            <td valign="top"> 
              <input name="par" type="text" id="par">
            </td>
            <td></td>
          </tr>
          <tr> 
            <td height="34" valign="top"><font color="#FF0000">Designer</font> 
            </td>
            <td valign="top"> 
              <input name="designer" type="text" id="designer">
            </td>
            <td></td>
          </tr>
          <tr> 
            <td height="34" valign="top"><font color="#FF0000">Course Distance</font> 
            </td>
            <td valign="top"> 
              <input name="distance" type="text" id="distance">
            </td>
            <td></td>
          </tr>
          <tr> 
            <td height="34" valign="top"><font color="#FF0000">Fairways Grass</font> 
            </td>
            <td valign="top"> 
              <input name="fairways" type="text" id="fairways">
            </td>
            <td></td>
          </tr>
          <tr> 
            <td height="34" valign="top"><font color="#FF0000">Greens Grass</font> 
            </td>
            <td valign="top"> 
              <input name="greens" type="text" id="greens">
            </td>
            <td></td>
          </tr>
          <tr> 
            <td height="34" valign="top"><font color="#FF0000">Website </font></td>
            <td valign="top"> 
              <input name="web" type="text" id="web" class="optional defaultInvalid url">
              <span class="example">http://www.example.com</span></td>
            <td></td>
          </tr>
          <tr> 
            <td height="25" colspan="2" valign="top">&nbsp;</td>
            <td></td>
          </tr>
          <tr> 
            <td height="34" valign="top"><font color="#FF0000">Email Address:</font><span class="required"><font color="#CC0000">*</font></span> 
            </td>
            <td valign="top"> 
              <input name="usr_email" type="text" id="usr_email">
            </td>
            <td></td>
          </tr>
          <tr> 
            <td valign="top" height="41"><font color="#FF0000">Upload Avatar</font></td>
            <td valign="top"> 
              <input type="hidden" name="size" value="350000">
              <input type="file" name="photo">
            </td>
            <td></td>
          </tr>
          <tr> 
            <td height="34"></td>
            <td></td>
            <td></td>
          </tr>
          <tr> 
            <td height="72" valign="top"> 
              <input TYPE="submit" name="upload" title="Add data to the Database" value="Add Course"/>
            </td>
            <td></td>
            <td></td>
          </tr>
        </table>
        <p align="center">&nbsp; </p>
      </form>
     
	   
      </td>
    <td width="196" valign="top">&nbsp;</td>
  </tr>
  <tr> 
    <td colspan="3">&nbsp;</td>
  </tr>
</table>

</body>
</html>
and the second part:

Code:
 <?php
include "../dbc.php";

//This is the directory where images will be saved
$target = "http://www.supremeicon.co.za/market/Thumbs/";
$target = $target . basename( $_FILES['photo']['name']);

//This gets all the other information from the form
$course=$_POST['course'];
$address=$_POST['address'];
$profile=$_POST['profile'];
$contact=$_POST['contact'];
$country=$_POST['country'];
$state=$_POST['state'];
$city=$_POST['city'];
$tel=$_POST['tel'];
$holes=$_POST['holes'];
$par=$_POST['par'];
$designer=$_POST['designer'];
$distance=$_POST['distance'];
$fairways=$_POST['fairways'];
$greens=$_POST['greens'];
$web=$_POST['web'];
$user_email=$_POST['user_email'];
$pic=($_FILES['photo']['name']);

//Writes the information to the database
mysql_query("INSERT INTO golflist (course,address,profile,contact,country,state,city,tel,holes,par,designer,distance,fairways,greens,web,user_email,photo,date)
VALUES ('$course', '$address', '$profile','$contact','$country','$state','$city','$tel','$holes','$par','$designer','$distance','$fairways','$greens','$web','$user_email','$pic',now())") ;

//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.";
}
?>
Can anybody see where I am going wrong here? If not everywhere

Thanks for your time. I do appreciate it!
Reply With Quote
  #2 (permalink)  
Old October 26th, 2009, 05:50 AM
Authorized User
 
Join Date: Jul 2008
Location: , , India.
Posts: 18
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Check your destination folder permission!
It should be as 705(in Linux sys), writable
Reply With Quote
Reply


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 Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Upload image-create & save thumbnail-display image angshujit ASP.NET 2.0 Professional 6 July 11th, 2013 10:34 PM
how to upload image into sql server database avnishraj23 General .NET 1 December 4th, 2007 01:20 PM
open image file from client then save to server hendyhanusin ASP.NET 1.0 and 1.1 Basics 11 March 22nd, 2007 07:28 AM
save an image ming C# 2005 0 July 11th, 2006 07:14 PM
display an image (more info) gorney .NET Web Services 0 December 29th, 2003 01:32 PM



All times are GMT -4. The time now is 09:06 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.