0

I'm having a wierd problem with PHP/MySQL.

I have uploaded into a BLOB a word document. Its successfuly and if (say using SQLYog) I open and save the BLOB as a file it will open perfectly in Open Writer.

However the minute I try to output the data through PHP I get a corrupt document.

I'm using the following to do the output:

header('Content-length: '.$file['FileSize']);
header('Content-Type: '.$file['FileSize']);
header('Content-Disposition:attachment; filename="'.$file['FileName'].'"');
echo $file['Content'];

Any help would be great.

Thanks

Antony

2 Answers2

2

What are you trying to do here?:

header('Content-Type: '.$file['FileSize']);

Content type should specify the type of the file (or, in strict HTTP terms, the response body), not the size of it. For a Word document, I believe the content type would be:

"application/ms-word"
David
  • 208,112
  • 36
  • 198
  • 279
  • I'm making progress. It looks like there is extra white space added at the beginning of the output from PHP that is not there in the original or when saving from MySQL. Not sure why and a trim doesn't fix it so keeping on investigating. – user1924004 Apr 17 '13 at 16:16
  • @user1924004: Is there more to the PHP file than the 4 lines shown in the question? `echo` doesn't replace the entire response with the file contents, so the rest of the PHP file may be corrupting the response with a whitespace character somewhere. Is the corruption just from a few extra characters or is the entire binary response incorrect? Maybe try something other than `echo` as well? `fwrite` perhaps? – David Apr 17 '13 at 16:20
  • I've done an echo 'hello world' and for some reason there is whitespace appearing before that too! – user1924004 Apr 17 '13 at 16:23
  • 1
    FIXED IT!! I had bloomin whitespace after my last ?> in the Controller. Who on earth knows why that should migrate into the view but removed that and hey presto. Whitespace was my problem. Thanks to all the replies and sorry this isn't an official answer - I have 7 hours to go! – user1924004 Apr 17 '13 at 16:33
0
// define results into variables
$name=$row['task_name'];
$size=$row['task_size'];
$type=$row['task_type'];
$content=$row['task_content'];

// give our picture the proper headers...otherwise our page will be confused
header("Content-Disposition: attachment; filename=$name");
header("Content-Disposition: filename=$name");  
header("Content-length: $size");
header("Content-type: $type");
echo $content;
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324