Working code to insert images into MySQL (success!)
| From: | Mark Gordon | Date: | Mon, 30 Jul 2001 02:41:10 +0000 |
| Subject: | Working code to insert images into MySQL (success!) | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-10887@lists.php.net to get a copy of this message | ||
I worked all day on this, so I thought it might be useful to others.
Its adapted from Florian Dittmer's article on PHPBuilder.com:
http://phpbuilder.com/columns/florian19991014.php3
The script is for storing binary (image) data directly into a MySQL
table. I did not have to create a separate DATABASE - just a BLOB
table. Both types of Magic quotes are off.
My problem was that I like real lean code, and thought I could do
without things like filesize....nooooo
<html>
<body>
<form method="post"
action="binform.html"
enctype="multipart/form-data">
FILE DESCRIPTION:<br>
<input type="text" name="form_description">
<input type="hidden" name="max_file_size" value="750000">
<br>File to Upload/store in DB:<br>
<input type="file" name="form_data">
<p><input type=submit value=submit>
</form>
<?php
include ("/home/mgordon/php/wamex.inc"); //insert your own connect info
dblink (); //and here
if ($form_data)
{
$data=addslashes(fread(fopen($form_data, "rb"), filesize($form_data)));
$result=mysql_query("
INSERT INTO binary_data(description, bin_data, filename, filesize,
filetype)
VALUES ('$form_description', '$data', '$form_data_name',
'$form_data_size', '$form_data_type')
");
$id=mysql_insert_id();
print "<p> This file has the following DB ID:<b>$id</b>";
}
?>
</body>
</html>
=====
Mark
eyesyte@yahoo.com
__________________________________________________
Do You Yahoo!?
Make international calls for as low as $.04/minute with Yahoo! Messenger
http://phonecard.yahoo.com/