Jump to content


Photo

Upload image and text via form


  • Please log in to reply
3 replies to this topic

#1 richardmlaws

richardmlaws
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 03 March 2006 - 10:25 PM

Hi,

I am having trouble uploading some text and an image to a MYSQL database via a form. I can upload the text fine but "no go" with the image.

I am not a great PHP coder so I wouldn't be suprised if this is all wrong. Any help would be greatly appreciated.

I'm not too worried about security at the moment thats why I have got the connection settings in there.

Here is the code I have been using...

=============================================================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR...ml4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

<?
if($_POST['submit'])
{
mysql_connect("host","user","pass");

mysql_select_db("db") OR DIE ("Unable to select db".mysql_error());

$id = $_POST['id'];
$description = $_POST['description'];
$date = $_POST['date'];

$imgData = addslashes(fread(fopen($_FILES['userfile']['tmp_name'], "r"),
filesize($_FILES['userfile']['tmp_name']))) or die("Could not open file.");

$result=MYSQL_QUERY("INSERT INTO table (id, description, date, image, filename, filesize, filetype)".
"VALUES ('$id','$description','$date','$imgData','$_FILES['image']['filename']','$_FILES['image']['filesize']','$_FILES['image']['filetype']')");
}
?>

<font face="verdana">

<h3><center><u>Add Equipment Record</u></center</h3>

<form enctype="multipart/form-data" action="" method="POST">


<TABLE WIDTH="100%" CELLPADDING="10" CELLSPACING="0" BORDER="2" RULES=NONE FRAME=BOX BGCOLOR="#FAEBD7">

<col span="1" align="right">

<TR>
<TD><font face="verdana"><b>Equipment ID:</b></font></TD>
<TD><INPUT TYPE='TEXT' NAME='id' VALUE='' size=60></TD>
</TR>
<TR>
<TD><font face="verdana"><b>Description:</b></font></TD>
<TD><INPUT TYPE='TEXT' NAME='description' VALUE='' size=60></TD>
</TR><br>
<TR>
<TD><font face="verdana"><b>Date:</b></font><br><font face="verdana" size="1"><left>(yyyy-mm-dd)</left></font></TD>
<TD>
<INPUT TYPE='TEXT' NAME='date' size=60>
</TD>
</TR>
<tr>
<td><font face="verdana"><b>Image:</b></font></td>
<td><input type="hidden" name="MAX_FILE_SIZE" value="2000000"><input name="userfile[]" type="file" class="box" id="userfile"></td>
</tr>
<tr>
<td>&nbsp</td>
<td><input name="upload" type="submit" id="upload" value="Submit"></td>
</tr>
</TABLE>

</form>

</font>

</body>
</html>
=============================================================

Thanks for any help

#2 michaellunsford

michaellunsford
  • Members
  • PipPipPip
  • Advanced Member
  • 1,023 posts
  • LocationLouisiana, USA

Posted 04 March 2006 - 01:05 AM

[!--quoteo(post=351444:date=Mar 3 2006, 04:25 PM:name=richardmlaws)--][div class=\'quotetop\']QUOTE(richardmlaws @ Mar 3 2006, 04:25 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
$imgData = addslashes(fread(fopen($_FILES['userfile']['tmp_name'], "r"),
[/quote]

try

$imgData = mysql_real_escape_string(file_get_contents($_FILES['userfile']['tmp_name']));

Also, you're using blob, right?

#3 richardmlaws

richardmlaws
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 04 March 2006 - 01:23 PM

hi michael

thanks for the reply but unfortunately its still not working.

my table has the following attributes and data types.

id - int(5)
description -varchar(100)
date - date
image - longblob
filename - varchar(50)
filesize - int(10)
filetype - varchar(50)

My PHP is looking like this now...

==============================================================
<?
if($_POST['submit'])
{
mysql_connect("host","user","pass");

mysql_select_db("db") OR DIE ("Unable to select db".mysql_error());

$id = $_POST['id'];
$description = $_POST['description'];
$date = $_POST['date'];

$imgData = mysql_real_escape_string(file_get_contents($_FILES['userfile']['tmp_name'])),
filesize($_FILES['userfile']['tmp_name']))) or die("Could not open file.");

$result=MYSQL_QUERY("INSERT INTO table (id, description, date, image, filename, filesize, filetype)".
"VALUES ('$id','$description','$date','$imgData','$_FILES['image']['filename']','$_FILES['image']['filesize']','$_FILES['image']['filetype']')");

}
?>
==============================================================
Thanks

#4 richardmlaws

richardmlaws
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 04 March 2006 - 09:33 PM

solved...

[a href=\"http://www.scit.wlv.ac.uk/~jphb/sst/php/extra/image1.html\" target=\"_blank\"]http://www.scit.wlv.ac.uk/~jphb/sst/php/extra/image1.html[/a]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users