Hello - any help appreciated!
I am trying to set up a document repository where files are uploaded into a MySQL table and then retrieved/downloaded. I am having a problem when I download Word, Excel, Powerpoint files in that the aplications state the file is not in the correct format. Note that I can see the data in the MySQL table after loading and it appears like normal Micrsoft type formatting...
The code I am using is -
UPLOAD:
DOWNLOAD:
Thanks for any help!
I am trying to set up a document repository where files are uploaded into a MySQL table and then retrieved/downloaded. I am having a problem when I download Word, Excel, Powerpoint files in that the aplications state the file is not in the correct format. Note that I can see the data in the MySQL table after loading and it appears like normal Micrsoft type formatting...
The code I am using is -
UPLOAD:
Code:
// copy uploaded file to into a variable
// for saving into MySQL
$fp = fopen($uploadedfile,'rb');
$file_content = fread($fp,filesize($uploadedfile));
fclose($fp);
$file_content = addslashes($file_content);
// Build the SQL statement to insert the
// file and associated information and then
// execute the statement
$sql = "INSERT INTO uploaded_files VALUES (null, '" . $uploadedfile_name . "', '" . $file_content . "', '" . $uploadedfile_size . "', '" . $uploadedfile_type . "', '" . $HTTP_POST_VARS["uploadedfiledesc"] .
"', curdate(), curtime())";
$sql_result = mysql_query($sql,$db_connection);
DOWNLOAD:
Code:
//retrieve the file data from MySQL table
$row = mysql_fetch_array($sql_result);
$sqlfilename = $row["filename"];//name $sqlfilesize = $row["filesize"];//size
$sqlfiletype = $row["filetype"];//MIME type
$sqlfile = $row["file"]; //actual file
// Build the MIME header/download file
header("Cache-control: private");
header("Content-length: " . $sqlfilesize);
header("Content-type: /*application/octet-stream*/" . $sqlfiletype);
header("Content-disposition: attachment; filename=\"" . $sqlfilename . "\"" );
$sqlfile=stripslashes($sqlfile);
print($sqlfile);
Thanks for any help!