Serving files form DB

From: Date: Tue, 09 Jan 2001 22:46:00 +0000
Subject: Serving files form DB
Groups: php.general 
Request: Send a blank email to php-general+get-33572@lists.php.net to get a copy of this message
I'm storing files in a mySQL table that looks like this: | file_name | varchar(100) | | MUL | | | | file_description | text | YES | | NULL | | | file_type | varchar(50) | | | | | | file_size | int(10) unsigned | | | 0 | | | added_by | varchar(10) | | | | | | access_types | text | YES | | NULL | | | access_users | text | YES | | NULL | | | file_num | bigint(10) unsigned | | PRI | 0 | auto_increment | | bin_data | blob | | | NULL | | the "file_type" field stores the actual data type for the file (such as 'application/msword', etc). To serve the file, I'm using the following code: <script language="php"> $dbInfo = unserialize( base64_decode( $db_key )); if( is_array( $dbInfo )) { $fileQuery = "SELECT * FROM $dbInfo[table_name] WHERE file_num=$dbInfo[record_num]"; $fileResult = mysql( $dbInfo[db_name], $fileQuery ); if(( $fileResult ) && ( mysql_errno() == 0 )) { $fileInfo = mysql_fetch_array( $fileResult ); header( "Content-Disposition: attachment; filename=$fileInfo[file_name]" ); header( "Content-Description: PHP Generated Data" ); header( "Content-type: $fileInfo[file_type];" ); header( "Content-length: $fileInfo[file_size]" ); echo $fileInfo[bin_data]; flush(); } } </script> Everything is working great. If I echo the query that is getting run, it's correct. If I echo out the data that is getting passed via the header() function, it looks all good. However, when my browser actually tries to download/open the file, everything gets all screwed up. The file name that it says that it's downloading is *nothing* like the actual file name and is in fact just a bunch of garbage. If I try to open anything other than a text file, I get errors from the application that is getting run (MSWord, say) and if I try to save if, the file is corrupt. There are no spaces or NULL characters being printed out by the script. What's going on? What am I doing wrong? Chris

« previous php.general (#33572) next »