problem upoading pictures and referencing them to a mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cs1h
Forum Newbie
Posts: 13
Joined: Fri Aug 31, 2007 11:39 am

problem upoading pictures and referencing them to a mysql

Post by cs1h »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi, 

I'm new to php and I can't get a script to work that will upload pictures from a form and save them to the server and then reference them in a mysql database. 

The code I have so far is as follows,

Code: Select all

<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

include 'library/config.php';
include 'library/opendb.php';

$query = "INSERT INTO `$menuFilesDMA$Catagory` (`Email`, `Title`, `Abstract`, `Article`, `name`, `size`, `type`, `content` ) ".
"VALUES ('$Email', '$title', '$message', '$messagetwo', '$fileName', '$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error, query failed'); 
include 'library/closedb.php';

echo "<br>File $fileName uploaded<br>";
} 
?>
If any one can help it would be much appriciated.

Thanks

Colin


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

General Posting Guidelines wrote:8. Don't just post - 'This doesn't work' - tell us exactly what doesn't work and make sure to include any error messages you're getting.
cs1h
Forum Newbie
Posts: 13
Joined: Fri Aug 31, 2007 11:39 am

Re: problem upoading pictures and referencing them to a mysq

Post by cs1h »

cs1h wrote:feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi, 

I'm new to php and I can't get a script to work that will upload pictures from a form and save them to the server and then reference them in a mysql database. 

The code I have so far is as follows,

Code: Select all

<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

include 'library/config.php';
include 'library/opendb.php';

$query = "INSERT INTO `$menuFilesDMA$Catagory` (`Email`, `Title`, `Abstract`, `Article`, `name`, `size`, `type`, `content` ) ".
"VALUES ('$Email', '$title', '$message', '$messagetwo', '$fileName', '$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error, query failed'); 
include 'library/closedb.php';

echo "<br>File $fileName uploaded<br>";
} 
?>
If any one can help it would be much appriciated.

Thanks

Colin


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color][/quote]

The script at the moment is not sending any data to the database but no error messages are cumming up.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Lets see your form.

Having not seen your form I would say that you need to specify the following form encoding inside the <form> declaration:

Code: Select all

enctype="multipart/form-data"
cs1h
Forum Newbie
Posts: 13
Joined: Fri Aug 31, 2007 11:39 am

Post by cs1h »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi, thanks for your reply, 

The form is as follows, 

[syntax="html"]<FORM name=myform action=send-mail.php method=post enctype="multipart/form-data">
            <table border="0" width="588" cellspacing="0" cellpadding="0" id="table9">
				<tr>
					<td width="574">
					<table border="0" width="574" cellspacing="0" cellpadding="0" id="table10">
						<tr>
							<td width="14" height="28" background="line_left_corner_top.png">&nbsp;</td>
							<td height="28" background="line_top.png" width="546" colspan="3">&nbsp;</td>
							<td width="14" height="28" background="line_right_corner_top.png">&nbsp;</td>
						</tr>
						<tr>
							<td width="14" background="line_left.png" rowspan="35" bgcolor="#FFFFFF">&nbsp;</td>
							<td colspan="3" width="546" bgcolor="#FFFFFF"><font color="#00518D"><span class="style10">
							Submit Articles to Roaming Hordes Magazine</span></font></td>
						  <td width="14" background="line_right.png" rowspan="35" bgcolor="#FFFFFF">&nbsp;</td>
						</tr>
						<tr>
							<td colspan="3" width="546" bgcolor="#FFFFFF">&nbsp;</td>
						</tr>
						<tr>
							<td colspan="3" width="546" bgcolor="#FFFFFF"><span class="style51">
							Adding to the members' magazine is easy. Simply fill 
							out the form below to submit an article <small>(all fields are 
							required to submit). </small></span></td>
						</tr>
						<tr>
						  <td colspan="3" bgcolor="#FFFFFF">&nbsp;</td>
					    </tr>
						<tr>
						  <td bgcolor="#FFFFFF"><span class="style68"><font color="#EE7F01">1</font></span><span class="style62"><font color="#EE7F01">.</font></span><span class="style62"> E<span class="style78">mail:</span></span></td>
					      <td bgcolor="#FFFFFF">&nbsp;</td>
					      <td bgcolor="#FFFFFF"><label>
					        <input name="Email" type="text" id="Email" size="28" maxlength="250">
					      </label></td>
						</tr>
						<tr>
							<td colspan="3" width="546" bgcolor="#FFFFFF">&nbsp;</td>
						</tr>
						<tr>
							<td width="546" align="left" valign="top" bgcolor="#FFFFFF"><span class="style68"><font color="#EE7F01">2</font></span><span class="style62"><font color="#EE7F01">.</font></span> <span class="style62">T<span class="style78">itle:</span></span></td>
						    <td width="546" bgcolor="#FFFFFF">&nbsp;</td>
						    <td style="width: 421px" align="left" valign="top" bgcolor="#FFFFFF">
							  <input name="title" type="text" class="userpass" id="title" style="width: 421" value="" size="28" maxlength="250"></td>
						</tr>
						<tr>
							<td width="546" bgcolor="#FFFFFF">&nbsp;</td>
							<td width="546" bgcolor="#FFFFFF">&nbsp;</td>
						    <td style="width: 421px" align="left" valign="top" bgcolor="#FFFFFF">&nbsp;							</td>
						</tr>
						<tr>
							<td width="546" height="20" bgcolor="#FFFFFF">
							<span class="style58"><font color="#000000">(20 
						  character limit)</font></span></td>
							<td width="546" height="20" bgcolor="#FFFFFF">&nbsp;</td>
						    <td style="width: 421px" align="left" valign="top" height="20" bgcolor="#FFFFFF">
							<span class="style58"><font color="#00518D"><em>i.e.</em>							catchy description of article's content</font></span></td>
						</tr>
						<tr>
							<td colspan="3" bgcolor="#FFFFFF">&nbsp;</td>
						</tr>
						<tr>
							<td colspan="3" bgcolor="#FFFFFF">&nbsp;</td>
						</tr>
						<tr>
							<td align="left" valign="top" width="546" bgcolor="#FFFFFF">
							  <span class="style68"><font color="#EE7F01">3</font></span><span class="style62"><font color="#EE7F01">.</font>
							A<span class="style78">bstract:</span></span>
							  <p class="style58">
							  <font color="#00518D"><em>i.e.</em> short overview and 
								introduction to full article</font></p>
							<p class="style58"><font color="#000000">(300 
							character limit)</font></p>
						  <p>&nbsp;</td>
						  <td align="left" valign="top" width="546" bgcolor="#FFFFFF">&nbsp;</td>
						  <td width="546" style="width: 421" bgcolor="#FFFFFF"><font size="1" face="arial, helvetica, sans-serif"><br>
<textarea name=message wrap=physical cols=50 rows=7 onKeyDown="textCounter(this.form.message,this.form.remLen,300);" onKeyUp="textCounter(this.form.message,this.form.remLen,300);"></textarea>
<br>
<input readonly type=text name=remLen size=3 maxlength=3 value="300"> 
<font face="Verdana, Arial, Helvetica, sans-serif">characters left</font></font><font face="Verdana, Arial, Helvetica, sans-serif">&nbsp;</font>&nbsp;</td>
						</tr>
						<tr>
							<td colspan="3" bgcolor="#FFFFFF">&nbsp;</td>
						</tr>
						<tr>
							<td colspan="3" bgcolor="#FFFFFF">&nbsp;</td>
						</tr>
						<tr>
							<td align="left" valign="top" width="546" bgcolor="#FFFFFF"><span class="style68"><font color="#EE7F01">4</font></span><span class="style62"><font color="#EE7F01">.</font></span> <span class="style62">A<span class="style78">rticle:</span></span>
							  <p><span class="style58">
							<font color="#00518D"><em>i.e.</em> full article</font></span></p>
							<p><span class="style58"><font color="#000000">(4000 
							character limit)</font></span></p>
						  <p>&nbsp;</td>
						  <td align="left" valign="top" width="546" bgcolor="#FFFFFF">&nbsp;</td>
						  <td width="546" bgcolor="#FFFFFF"><font size="1" face="arial, helvetica, sans-serif"><br>
        <textarea name=messagetwo wrap=physical cols=50 rows=7 onkeydown="textCounter(this.form.messagetwo,this.form.remLen2,4000);" onkeyup="textCounter(this.form.messagetwo,this.form.remLen2,4000);"></textarea>
        <br>
<input readonly type=text name=remLen2 size=4 maxlength=4 value="4000"> characters left</font>&nbsp;</td>
					  </tr>
						<tr>
							<td align="left" valign="top" width="546" bgcolor="#FFFFFF">&nbsp;</td>
						  <td align="left" valign="top" width="546" bgcolor="#FFFFFF">&nbsp;</td>
						  <td width="546" bgcolor="#FFFFFF">&nbsp;							</td>
						</tr>
						<tr>
							<td align="left" valign="top" width="546" bgcolor="#FFFFFF">&nbsp;</td>
						  <td align="left" valign="top" width="546" bgcolor="#FFFFFF">&nbsp;</td>
						  <td width="546" bgcolor="#FFFFFF">
							<span class="style86"><font color="#EE7F01">>></font>
							<font color="#00518D"><a href="add%20item.html">
							<font color="#00518D">Save article to personal email 
							account</font></a> </font><font color="#EE7F01">>></font><font color="#EE7F01"></font><font color="#EE7F01"></font></span></td>
					  </tr>
						<tr>
							<td width="546" colspan="3" bgcolor="#FFFFFF">&nbsp;</td>
						</tr>
						<tr>
							<td width="546" bgcolor="#FFFFFF"><span class="style68"><font color="#EE7F01">5</font></span><span class="style62"><font color="#EE7F01">.</font></span> <span class="style62">L<span class="style78">ocation:</span></span></td>
							<td width="546" bgcolor="#FFFFFF">&nbsp;</td>
						  <td align="left" valign="top" width="546" bgcolor="#FFFFFF"><select name="menuSubjectsDMA" onChange="changeFilesDMA()">
  <option value="none">Select a Continent
  <option value="">Africa
  <option value="">Asia
  <option value="">Europe
  <option value="">North America
  <option value="">Oceania
  <option value="">South America
  </select>

<!-- this menu holds the files to go-->

<select name="menuFilesDMA" style="width:300">

<option value="none"> Choose Country
</select>&nbsp;</td>
						</tr>
						<tr>
							<td width="546" bgcolor="#FFFFFF">&nbsp;</td>
							<td width="546" bgcolor="#FFFFFF">&nbsp;</td>
						  <td align="left" valign="top" width="546" bgcolor="#FFFFFF">&nbsp;							</td>
						</tr>
						<tr>
							<td width="546" bgcolor="#FFFFFF"><span class="style58">
							<font color="#000000">(20 character limit)</font></span></td>
							<td width="546" bgcolor="#FFFFFF">&nbsp;</td>
						  <td align="left" valign="top" width="546" bgcolor="#FFFFFF">
							<span class="style58"><font color="#00518D"><em>i.e.</em>							
							where and when did the events you have written about 
							occur? </font></span></td>
						</tr>
						<tr>
							<td width="546" bgcolor="#FFFFFF">&nbsp;</td>
							<td width="546" bgcolor="#FFFFFF">&nbsp;</td>
						  <td align="left" valign="top" width="546" bgcolor="#FFFFFF">&nbsp;							</td>
						</tr>
						<tr>
						  <td bgcolor="#FFFFFF"><span class="style68"><font color="#EE7F01">6</font></span><span class="style62"><font color="#EE7F01">. </font> P<span class="style78">hotos:</span></span></td>
						  <td bgcolor="#FFFFFF">&nbsp;</td>
						  <td align="left" valign="top" bgcolor="#FFFFFF"><label>
						    <input name="userfile" type="file" id="userfile"> 
						  </label></td>
					    </tr>
						<tr>
						  <td bgcolor="#FFFFFF">&nbsp;</td>
						  <td bgcolor="#FFFFFF">&nbsp;</td>
						  <td align="left" valign="top" bgcolor="#FFFFFF">&nbsp;</td>
					  </tr>
						
						<tr>
							<td width="546" bgcolor="#FFFFFF">&nbsp;</td>
							<td width="546" bgcolor="#FFFFFF">&nbsp;</td>
						  <td align="left" valign="top" width="546" bgcolor="#FFFFFF">&nbsp;							</td>
						</tr>
						<tr>
							<td width="546" height="42" bgcolor="#FFFFFF"><span class="style68">
							<font color="#EE7F01">7</font></span><span class="style62"><font color="#EE7F01">. </font>

							C<span class="style78">atagory:</span></span></td>
						  <td width="546" bgcolor="#FFFFFF">&nbsp;</td>
					      <td align="left" valign="middle" width="546" bgcolor="#FFFFFF"><select name="Catagory" id="Catagory">
                            <option>Magazine</option>
                            <option>News</option>
                            <option>Review</option>
                            <option>List</option>
                          </select></td>
					  </tr>
						<tr>
							<td width="546" bgcolor="#FFFFFF">&nbsp;</td>
							<td width="546" bgcolor="#FFFFFF">&nbsp;</td>
						  <td align="left" valign="top" width="546" bgcolor="#FFFFFF">&nbsp;							</td>
						</tr>
						<tr>
							<td width="546" height="20" bgcolor="#FFFFFF">&nbsp;</td>
						  <td width="546" height="20" bgcolor="#FFFFFF">&nbsp;</td>
						  <td align="left" valign="top" width="546" height="20" bgcolor="#FFFFFF">
							<span class="style18">
							<a href="catagories.html"><font color="#00518D">Choose the c</font></a></span><span class="style10"><a href="catagories.html"><font color="#00518D">atagory that best suits your article (if unsure click here)</font></a></span> </td>
					  </tr>
						<tr>
							<td width="546" height="19" bgcolor="#FFFFFF">&nbsp;</td>
						  <td width="546" height="19" bgcolor="#FFFFFF">&nbsp;</td>
						  <td align="left" valign="top" width="546" height="19" bgcolor="#FFFFFF">&nbsp;</td>
					  </tr>
						<tr>
							<td width="546" height="19" bgcolor="#FFFFFF">&nbsp;</td>
						  <td width="546" height="19" bgcolor="#FFFFFF">&nbsp;</td>
						  <td align="left" valign="top" width="546" height="19" bgcolor="#FFFFFF">&nbsp;							</td>
					  </tr>
						<tr>
							<td width="546" height="20" bgcolor="#FFFFFF"><font color="#EE7F01">
							<span class="style68">8.</span> </font>
							<span class="style62">S<span class="style78">ubmit:</span></span></td>
							<td width="546" height="20" bgcolor="#FFFFFF">&nbsp;</td>
						  <td align="left" valign="top" width="546" height="20" bgcolor="#FFFFFF"><span class="style51">By submitting you agree to the <span class="style17">							<a href="register.html"><font color="#00518D">terms 
						  of use</font></a></span> and <span class="style17">							<a href="register.html"><font color="#00518D">							privacy policy</font></a></span>.</span></td>
					  </tr>
						<tr>
							<td width="546" height="20" bgcolor="#FFFFFF">&nbsp;</td>
							<td width="546" height="20" bgcolor="#FFFFFF">&nbsp;</td>
						  <td align="left" valign="top" width="546" height="20" bgcolor="#FFFFFF">&nbsp;							</td>
						</tr>
						<tr>
							<td width="546" height="20" bgcolor="#FFFFFF">&nbsp;</td>
							<td width="546" height="20" bgcolor="#FFFFFF">&nbsp;</td>
						  <td align="left" valign="top" width="546" height="20" bgcolor="#FFFFFF"><label>
						    <INPUT type=submit value=Submit name=submit>
						  </label></td>
						</tr>
						<tr>
							<td width="546" height="20" bgcolor="#FFFFFF">&nbsp;</td>
							<td width="546" height="20" bgcolor="#FFFFFF">&nbsp;</td>
						  <td align="left" valign="top" width="546" height="20" bgcolor="#FFFFFF">&nbsp;							</td>
						</tr>
						<tr>
							<td width="546" height="20" bgcolor="#FFFFFF">&nbsp;</td>
							<td width="546" height="20" bgcolor="#FFFFFF">&nbsp;</td>
						  <td align="left" valign="top" width="546" height="20" bgcolor="#FFFFFF">
							<span class="style51">
							Having problems with a submission? Email
							    <a href="mailto:info@facebook.com?subject=Facebook%20Registration">
							info@myroho.com</a>.</span></td>
						</tr>
						<tr>
							<td width="14" height="28" background="line_left_corner.png" bgcolor="#FFFFFF">&nbsp;</td>
							<td height="28" width="546" background="line_base.png" colspan="3" bgcolor="#FFFFFF">&nbsp;</td>
							<td width="14" height="28" background="line_right_corner.png" bgcolor="#FFFFFF">&nbsp;</td>
						</tr>
					</table>
				  </td>
					<td width="14">&nbsp;</td>
				</tr>
				<tr>

					<td width="574">&nbsp;</td>
					<td width="14">&nbsp;</td>
				</tr>
			</table></form>
The other files I use are

formproccesor.php[/syntax]

Code: Select all

<?php
if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0)
{
$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

include 'library/config.php';
include 'library/opendb.php';

$query = "INSERT INTO `$menuFilesDMA$Catagory` (`Email`, `Title`, `Abstract`, `Article`, `Photo_name`, `Photo_size`, `Photo_type`, `Photo` ) ".
"VALUES ('$Email', '$title', '$message', '$messagetwo', '$fileName', '$fileSize', '$fileType', '$content')";

mysql_query($query) or die('Error, query failed'); 
include 'library/closedb.php';

echo "<br>File $fileName uploaded<br>";
} 
?>
config.php

Code: Select all

<?php
// This is an example of config.php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'password';
$dbname = 'somedatabase';
?>
and opendb.php

Code: Select all

<?php
// This is an example opendb.php
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
mysql_select_db($dbname);
?>

Thank you,

Colin


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

That amount of unformatted code is hard to read. Please use

Code: Select all

tags when posting php code. see http://forums.devnetwork.net/faq.php?mode=bbcode


Please try

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors', true);

echo '<pre>_POST: ', print_r($_POST, true), "</pre>";
echo '<pre>_FILES: ', print_r($_FILES, true), "</pre>";
if ( get_magic_quotes_gpc() ) die('magic_quotes_gpc on. bull');

if ( !isset($_POST['upload'], $_FILES['userfile']) ) {
  echo '!isset($_POST[upload], $_FILES[userfile])';
}
else if ( $_FILES['userfile']['size'] <= 0 ) {
  echo '$_FILES[userfile][size] <= 0';
}
else {
  $content = file_read_contents($_FILES['userfile']['tmp_name'])
      or die('cannot read contents from '.$_FILES['userfile']['tmp_name']);
  
  require 'library/config.php';
  require 'library/opendb.php';

  // ... later ...
  
  require 'library/closedb.php';

  echo '<div>File "', htmlentities($_FILES['userfile']['name']),'" uploaded</div>';
}
?>
as formproccesor.php
Post Reply