Jump to content

Upload image and text via form


richardmlaws

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.