Hi Joeore,
1.) Set up a field containing a unique ID
2.) Set up a BLOG field for the file data.
3.) Set up a VARCHAR mime type field if you will be storing more than one type of file.
Optional.. add fields for file name or other details.
Basically you need to extract the binary data from the file using a few PHP functions, addslashes, file will be kept in a normal variable, and then inserted like you would insert any other type of data.
// Read the contents of the file into a variable, addslashes for DB insertion
// where 'userfile' is the name of the upload field
// file_get_contents PHP 4 >= 4.3.0
$file = addslashes(file_get_contents($_FILES['userfile']['tmp_name']));
$mime = $_FILES['userfile']['type'];
if (false === ($query = mysql_query("INSERT INTO `files` VALUES(null, '{$file}', '{$mime}')")))
{
echo 'insertion failed';
}
Here is another method which does the same as the above:
// Read the contents of the file into a variable, addslashes for DB insertion.
$file = addslashes(fread(fopen($_FILES['userfile']['tmp_name'], 'r'), filesize($_FILES['userfile']['tmp_name'])));
The following is what you do to retrieve the contents of the file:
<?php
// if passing the ID via GET
if (isset($_GET['id']))
{
$id = $_GET['id'];
}
// get_file.php
// Make SELECT query
$data = mysql_fetch_array(mysql_query("SELECT `file`, `mime` FROM `files` WHERE `id` = '{$id}'"), MYSQL_ASSOC);
// Set the content type header
header('Content-type: {$data['mime']}');
// Also notice that I am not stripping the slashes,
// Doing so may corrupt data in certain file types,
// while you may need to do so for others.
echo $data['file'];
?>
Then simply include a link to the PHP file...
For images,
<img src='get_file.php?id=5' />
Be extra careful about resource consumption here, it may be both more efficient and practical to simply store the file in a directory file system!
: )
Rich
:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::