First off some clarification, I break the file up into 64k chunks and store each chunk as a record in the db. Here is the code...
The Code to upload the file into the database in pieces
if($action == "add"){ //adding file to library
// File Information
$SrcPathName = $HTTP_POST_FILES["file_to_add"]["tmp_name"];
$SrcFileType = $HTTP_POST_FILES["file_to_add"]["type"];
$DstFileName = $HTTP_POST_FILES["file_to_add"]["name"];
//check permissions
$allow_query = mysql_query("SELECT * FROM projectUsers WHERE userID = '$curr_user' AND projectID = '$curr_project'");
$allow_results = mysql_num_rows($allow_query);
if((mysql_num_rows($allow_query) > 0) || ($sess_access_level == "admin")){ //user has permission to upload file
if($SrcPathName){
if(!file_exists($SrcPathName)){
$error = "ERROR: File Was Not Uploaded Correctly";
}
else{
$fp = fopen($SrcPathName, "r");
if(!$fp){
$error = "ERROR: Cannot open uploaded file";
}
else{
//ok to insert file into database
$extension = "";
$filename = "";
$filesize = filesize($SrcPathName);
$name_array = explode(".", $DstFileName);
$num_str = count($name_array);
if($num_str > 1){
end($name_array);
$extension = current($name_array);
$explode_arg = ".";
$explode_arg .= $extension;
$just_name_array = explode($explode_arg, $DstFileName);
$filename = $just_name_array[0];
}
$time = date("Y-m-d H:i:00");
$add_result = mysql_query("INSERT INTO documents VALUES('','$curr_project','$filename','$SrcFileType','$filesize',
'$curr_user','1','$time','$time','$curr_user','0','','$extension','no','$parent')");
if(mysql_errno() == 0) {
$fileid = mysql_insert_id();
while(!feof($fp)) {
$binarydata = addslashes(fread($fp,65535));
// $binarydata = mysql_escape_string(fread($fp,65535));
$SQL = "insert into document_store (masterid, filedata)";
$SQL .= " values ( " . $fileid . ", '" . $binarydata . "' )";
if(!mysql_query($SQL,$dbh)) {
$error .= "Document $DstFileName corrupted: Insert failed: " . mysql_error();
$del = "delete from documents where id=$fileid";
mysql_query($del,$dbh);
$del = "delete from document_store where masterid=$fileid";
mysql_query($del,$dbh);
} //end if
} //end while
} //end if
else {
// default: //error occurred adding file information
$error = "ERROR: File information not inserted correctly.";
$error .= "<br>" . mysql_error();
}//end else
fclose($fp);
unlink($SrcPathName);
The Code to get the file from the database send it to the client
// Send down the header to the client
Header ( "Content-Type: $FileObj->type" );
Header ( "Content-Length: " . $FileObj->size );
Header ( "Content-Disposition: attachment; filename=$FileObj->name.$FileObj->extension" );
Header ( "Cache-Control: private" );
// Loop thru and stream the nodes 1 by 1
for ($Z = 0 ; $Z < count($nodelist) ; $Z++) {
$SQL = "select filedata from document_store where id = " . $nodelist[$Z];
if (!$RESX = mysql_query($SQL, $linkid)) {
die("Failure to retrive file node data: " . @mysql_error());
}
$DataObj = mysql_fetch_object($RESX);
echo $DataObj->filedata;
}
}
...Any Help?