Jump to content

Archived

This topic is now archived and is closed to further replies.

richardmlaws

Upload image and text via form

Recommended Posts

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/html4/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

Share this post


Link to post
Share on other sites
[!--quoteo(post=351444:date=Mar 3 2006, 04:25 PM:name=richardmlaws)--][div class=\'quotetop\']QUOTE(richardmlaws @ Mar 3 2006, 04:25 PM) [snapback]351444[/snapback][/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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.