Forum Moderators: coopster
I have been pulling my hair out for two days (sad, I know) trying to figure out where this problem is originating from. At this point I am desperate for a solution! Here is the code for upload:
$data = fread(fopen($file['tmp_name'], "r"), filesize($file['tmp_name']));
$data = base64_encode($data);
$file_name = $name;
$file_size = filesize($file['tmp_name']);
$file_type = $file['type'];
$insert = db_query("insert into files (id, bin_data, file_name, file_size, file_type) values('$this->pid', '$data', '$file_name', '$file_size', '$file_type')"); and download:
$query = @db_query("SELECT bin_data, file_type, file_name, file_size FROM files WHERE id = '$file_id'");
$result = db_fetch_object($query);
$file = base64_decode($result->bin_data);
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$result->file_name");
header("Content-length: $result->file_size");
print $file; I tried going back from PHP 4.3.7 to 4.3.4, but the problem persists. I thought it might be a bug. I am completely lost on this problem. Any ideas would be greatly appreciated!
I don't think Content-type: application/octet-stream is the correct MIME-type for an MS-Word file, so IE will revert to examining the content itself to try to figure our what kind of file it is and how to handle it. If the initial few bytes don't exactly match any of IE's 'templates' for various filetypes, it'll just treat it as text/html, and you'll get the 'raw' data printed out in the browser window.
The root problem is this rogue space character -- where is it coming from? The MIME-type problem just makes it worse, and will cause more problems in non-IE browsers, which strictly abide by the Content-type specified in the header. I think you should try deriving the correct filetype from your database field, and inserting it into the Content-type header. But again, where's the extra space character coming from?
Jim
The reason I'm using application/octet-stream is to force download (I've tested it in IE/Win and other browsers on Mac and it doesn't display in the browser window) which is a solution I've seen on various boards. I also have .txt and image files uploaded, and I want to ensure these are downloaded and not viewed directly in the browser.
The rogue space character in .doc and .xls files (and all others... images and txt as well) seems to remain whether I use the correct filetype or octet-stream.
I tried using
print substr($file, 1); but this strips the first character AFTER the space! It gives a clue that the problem is not with my code, but it doesn't seem to be with the filetype either.
Can anyone shed some light on this? I'm scanning all uploads for virii with ClamAV.. could this have anything to do with it? I wouldn't think so because this is done before storing in the DB..
Jim
Tthis is an area I have not been aware of until now so please excuse my ignorance... I had a look at mbstring functions in PHP. Are they relevant?
I have just done some tests. I uploaded the file to the server and wrote it immediately to a temporary file to see if the rogue shift was taking place between the upload and base64_encoding to save to the database. There was no rogue space. Then I wrote another temp file after downloading it from the database and base64_decoding.. again, no rogue space. The shift in the file content is occuring sometime during the actual copying of the file to the remote machine.
One thing I have noticed is that if I exclude the header pertaining to filesize, the HTML of the page that loads after the download is included in the content of the file. I'm not sure if this is causing the space at the start of the file though..
Weeeee how fun.