Page 1 of 1

File Download - Character Encoding seems to be going awry

Posted: Thu Jul 24, 2003 5:05 am
by mooberry
Hi there,

I am pulling my hair out over this problem, and I am getting fairly desperate to find out the (probably simple) explanation :x

I have programmed a PHP system to upload files to a BLOB field in a MySQL database. No problems there, the file uploads fine, and I can see it there. The problem is when I try to use the header() function to spit it back out to a browser, the file downloads fine, with the correct filename, etc, but is completely un-openable. The contents of the file are perfect and intact, my computer just has no idea what type of file it is.


I originally created the system to be served off an IIS server to an IE browser, and it worked fine there. It is only once I ported it to Apache on a Linux box that I am getting this problem. I am convinced that I must be missing something in my headers that would resolve this issue, but I can't think what.

It is worth mentioning that when receiving the file, Mozilla tells me that it knows it is a file of (in this case) application/msword. But when it is saved and I go and look at it, it is of type unknown.


The code for the download is:

Code: Select all

<?php

  header("Pragma: public");
  header("Cache-control: private");
  header("Content-Type: "  .$this->getMimeType());
  header("Content-Length: ".$this->getFileSize());
  if(!$streaming) {

    header("Content-Disposition: attachment;filename=" .$this->getFilename());
  }

  echo $this->getFileData();

?>
In the code above, $this is a custom object, with the various fields being self-explanatory. The object works 100%, that I am sure of, because this set of headers delivers teh file OK under IIS, but only fails under Apache.



Can anyone help me?


PS:
As an extra mention, the $streaming variable just makes sure that the browser is forced to download the content, not display or interpret it (as often happens with, say, images)


PPS:
If you want to check out the problem for yourself, it is on the downloads section of http://www.ferret.co.za

Posted: Thu Jul 24, 2003 9:18 am
by DuFF
I downloaded the Supermanual fine. The MSWord file opened up but I had the wrong version, MSWord 97, so it was a little messed up. Otherwise it seemed to work fine for me. I think this a default viewer problem or something.

BTW, the first quote on the left hand side of your site has slashes in it ("Wits'' glory days are over..) Just run stripslashes() on it if you're pulling it out of a database.

Posted: Thu Jul 24, 2003 9:53 am
by mooberry
Thanks, I will do that!

The problem persists though. What you experienced IS the problem. Somewhere in the actual sending of the file to the browser, it is losing somthing, so that when it arrives, it won't open in MS Word, even though it is a word doc. My suspicion is that storing it in a BLOB may be changing the file's encoding somehow, and that that is causing it to go screwey, but I don;t know how that could work.

Posted: Thu Jul 24, 2003 10:25 am
by mooberry
As near as I can get to solving this problem, it seems that the environment PHP is now running in is causing the file reading to be non-binary safe. I don't know why, and I don;t know how to fix it, but I am SURE this is what is wrong. The file read line in my code looks like this:

Code: Select all

<?php

@$this->setFileData(fread(fopen($_FILES["uploadedFile"]["tmp_name"], "rb"), fileSize($_FILES["uploadedFile"]["tmp_name"])));
?>
Where $this->setFileData() just copies the data to the object's property (gets & sets = good OO practice ;)

Posted: Thu Jul 24, 2003 10:50 am
by mooberry
...and I have checked get_magic_quotes(), and it is set to 0. I am using a MySQL database, and connecting to it using ADODB, but I don't think those are at fault. Is there any way that the file read itself is non-binary safe, despite my best efforts otehrwise?

I am getting desperate over this error!