Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems downloading MS application files from MySQL table

Status
Not open for further replies.

kwberg

IS-IT--Management
Feb 9, 2003
1
US
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:
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!
 
I would have recommended that you store the names of the files in MySQL, but the actual contents on the filesystem.

Did you know, by the way, that block-comment marks are treated a string literals inside quotes?

This line:
Code:
header("Content-type: /*application/octet-stream*/" . $sqlfiletype);
Is actually sending "/*appl...stream*/" plust the content-type you're retrieving from the database.
Want the best answers? Ask the best questions: TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top