You need to make sure you get the content of the file properly first,
see the other replies on detials of how to do this.
I will normally base64_encode a file before putting it into the database
just to make sure that it doesn't corrupt other bits of the database.
to output, just grab the file from the database, decode (if needed) and
output to browser.
Note that it's normally pretty useful to store the incoming Mime-Type of
the data in the database if your accepting more than one type of file so
that you can output the correct headers.
On another note, make sure you watch when uploading files, Mozilla etc
have a few quirks when uploading files.
Here's an extract from one of my recent scripts that handle file uploads.
-- BEGIN --
$get_magic_quotes=get_magic_quotes_gpc();
$input=parse_incoming();
if ($input['act']=='do')
{
if (is_array($HTTP_POST_FILES['config']))
{
$CONFIG_NAME = $HTTP_POST_FILES['config']['name'];
$CONFIG_SIZE = $HTTP_POST_FILES['config']['size'];
$CONFIG_TYPE = $HTTP_POST_FILES['config']['type'];
$CONFIG_TYPE = preg_replace( "/^(.+?);.*$/", "\\1", $CONFIG_TYPE );
$CONFIG_FILE = $HTTP_POST_FILES['config']['tmp_name'];
if (is_array($HTTP_POST_FILES['file'])) {
$FILE_TYPE = $HTTP_POST_FILES['file']['type'];
$FILE_TYPE = preg_replace( "/^(.+?);.*$/", "\\1", $FILE_TYPE );
}
if ($HTTP_POST_FILES['file']['name'] == "" or
!$HTTP_POST_FILES['file']['name'] or $HTTP_POST_FILES['file']['name'] ==
"none" or $HTTP_POST_FILES['config']['name'] == "" or
!$HTTP_POST_FILES['config']['name'] or
$HTTP_POST_FILES['config']['name'] == "none" ) {
die("you must include files in both upload fields");
}
}
else
{
die("you must include files in both upload fields");
}
if ($FILE_TYPE=="" || !$FILE_TYPE)
{
die ("Your file must be less than 2Mb in size!");
}
/*if ($FILE_TYPE=="application/octet-stream") {
die("Your browser did not provide a suitable mime-type for this type
of file.<br /><br />However, your conf_mime_types.php should hold a
default entry that will allow you to upload this file.");
}*/
if (!preg_match("/^conf_mime_types\.php/si",$CONFIG_NAME)) {
die("Your config file MUST be named conf_mime_types.php");
}
$allow_upload=$input['allow_upload']? 1 : 0;
$allow_avatar=$input['allow_avatar']? ",1": "";
if ((!$input['image']) || (!$input['desc'])) die("You forgot to enter
an image name or a description");
$line=" \"$FILE_TYPE\" => array ( $allow_upload,
'{$input['image']}', '{$input['desc']}' $allow_avatar) ,";
$file=join(file($CONFIG_FILE));
--END--
Though the way I handle the file here ( $file=join(file($CONFIG_FILE)) )
is a crude way of doing things, but was best for the job at hand.
Eric Kincl wrote:
Hello, its been a while since I posted/looked here... my normal email client
doesn't handle newsgroups :( (ximian evolution)
I was wondering how you stick a file into a database, and then retrive it
again for the user with PHP/MySQL. I tried the following which apparently
didnt work...
Very quick overview of what I did...
html
------
<input type="file" name="file"><input type="submit>
PHP
------
$SQL = "INSERT INTO table (file) VALUES (" + $_REQUEST['file'] + ");";
I didn't even bother running the SQL querry, I just echoed it and I got the
location of the file (ie: /home/eric/blah...)
How do I get the file into the database, and once its there, how do i get it
back out?
Thanks,
-Eric Kincl