Ok - basically to insert ANY file type into a MySQL database I wrote this - excuse the swearing in my code, but all my code is commented for myself to remind me of what a prat I am - this is basically an upload function, and correctly shows you how to use the LOAD_DATA thingy, which I've never seen anybody get working (though there are probably loads in this forum). The commented out block is the code I used to use before I got LOAD_FILE working. Yup, all I had to do was basically addslashes but I honestly am an idiot.
function check_upload()
{
if($_REQUEST['Submit'])
{
$fileName=$_FILES['filename']['name'];
$tempName=$_FILES['filename']['tmp_name'];
$fileType=$_FILES['filename']['type'];
$fileError=$_FILES['filename']['error'];
echo "File Name: $fileName - Temp Name: $tempName - File Type: $fileType - ERROR: $fileError<br>";
if ( is_uploaded_file( $tempName ) )// move_uploaded_file($tempName, ".xxxkillthis.txt") ) //if you wanted keepsies
{
$tempName=mysql_escape_string($tempName); //GAH! This is the key to all LOAD_DATA probs
$query="INSERT INTO files (filename,filetype,data) VALUES ('$fileName','$fileType',LOAD_FILE('$tempName') )";
$res=mysql_query($query);
if (!$res) { echo "No Insert: ".mysql_error()."<br>"; die("GOODBYE");}
/* This is the alternative UPDATE code
$query="INSERT INTO files (filename,filetype) VALUES ('$fileName','$fileType')";
if( mysql_affected_rows() > 0 )
{
$rowID=mysql_insert_id();
$query="UPDATE files SET data=LOAD_FILE('$tempName') WHERE fileid=$rowID";
$res=mysql_query($query) || die ("No LOAD_DATA - ".mysql_error());
echo "Afffected Rows post LOAD: ".mysql_affected_rows()."<br>";
}
else
{ //kill the empty row, if arsed
echo "Insert UP Shit Creek<br>";
}//*/
}
else
echo "<font color=red>File FAILED Uploading</font><br>";
}
}
Then in the showfile.php you just pump out a the appropriate header. You tell it which file by adding the id as a get variable in the link.
showfile.php?id=58
$fileID=$_REQUEST['id'];
$dbh=mysql_connect();
mysql_select_db("dmfiles") || die("Couldn't Select dmfiles DB");
$res=mysql_query("SELECT filename,filetype,data FROM files WHERE fileid=$fileID");
if($res==0) echo "<b>I am called </b> via MySQLInsertFile with an ID - that hasn't happened<br>
<br>".mysql_error();
$rows=mysql_num_rows($res);
if (!$rows) die("No Rows Found");
// list($name,$type,$data)=mysql_fetch_row($res);
$name=mysql_result($res,0,0);
$type=mysql_result($res,0,1);
$data=mysql_result($res,0,2) ;
// echo "NAME: $name - TYPE: $type - Length: ".strlen($data)."<br><br>";
header("Content-Type: $type");
header("Content-Disposition: inline;filename=$name");
header("Content-Length: ".strlen($data));
//*/
echo $data;
// echo mysql_result($res,0,2);
mysql_free_result($res);
mysql_close();
Sorry about the commented out bits, they're just alternative methods that I've binned, but left to remind how I did it.