Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Problems with HTTPRequest::send_file(), header issue


Reply


6 Posts   2728 Views

Avatar
jhirm

Community Member, 21 Posts

13 September 2009 at 6:51am

I'm trying to use the HTTPRequest class to send a file to the browser. The goal is ultimately to create a pdf dynamically, then send it to the user. The PDF creation works fine, but I can't get SS to use the correct header when it sends the file to the browser. I've tried pretty much everything I can think of. As far as I understand, this should be all I need:

In the controller:

public function SendFileTest() {
   return HTTPRequest::send_file(file_get_contents("../assets/test.pdf"), "test.pdf");
}

Looking at the HTTPRequest docs, it seems like SS should use the file extension to choose the correct mime type, but when I try this action in the browser it sends the file to the browser with a Content-Type of text/html (which displays the pdf as a bunch of gobbledygook in the browser window instead of giving the download prompt). So naturally I tried setting the headers manually, using the third parameter in the send_file() function, using the HTTPRequest::addHeader() function, and using straight up PHP header() function... all to no avail. SS still sends the content back with a Content-Type of text/html. What am I missing??

I've tried it with other file types as a test as well, nothin' doin'.

I know the HTTPRequest::send_file() works, since it's how the TableListField exports to csv. Any help would be much appreciated.

Avatar
Fuzz10

Community Member, 787 Posts

8 February 2011 at 9:10pm

Did you ever solve this problem ?

Avatar
Hamish

Community Member, 712 Posts

10 February 2011 at 11:49am

Hi there,

I don't have any issues with pdfs on Windows or Linux, so could you post your OS and SilverStripe version? Also, are there any other Apache modules that might be getting in the way? Also, what does the code around send_file look like?

Hamish

Avatar
Fuzz10

Community Member, 787 Posts

10 February 2011 at 11:31pm

Hamish,

PDF's work alright , other files do not. It's very , very weird..

I fixed it for now , by installing Mod_xsendfile and removing some header info. If I enable the last 2 lines again ,the system breaks and starts corrupting headers (except from PDF files ..) ...

// Normal operation:
      $mimeType = HTTP::getMimeType($file_name);
      header("Content-Type: {$mimeType}; name=\"" . addslashes($file_name) . "\"");
      header("Content-Disposition: attachment; filename=" . addslashes($file_name));
//      header("Content-Length: {$file_size}");
//      header("Pragma: ");

Silverstripe 2.4.x , no weird apache modules installed , running on CentOS , many other silverstripe sites running on that platform ...

Avatar
Hamish

Community Member, 712 Posts

11 February 2011 at 9:15am

Hmm, maybe the file size is being incorrectly reported. Is it both of those lines or just one of them?

Avatar
Aaron Brockhurst

Community Member, 30 Posts

24 April 2012 at 1:04am

Did anyone get to the bottom of this? We've seen the same issue in 2.4.5

Aaron