Jump to:

22975 Posts in 11620 Topics by 2826 members

General Questions

SilverStripe Forums » General Questions » Problems with HTTPRequest::send_file(), header issue

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

Page: 1
Go to End
Author Topic: 2435 Views
  • jhirm
    Avatar
    Community Member
    21 Posts

    Problems with HTTPRequest::send_file(), header issue Link to this post

    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.

  • Fuzz10
    Avatar
    Community Member
    786 Posts

    Re: Problems with HTTPRequest::send_file(), header issue Link to this post

    Did you ever solve this problem ?

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Problems with HTTPRequest::send_file(), header issue Link to this post

    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

  • Fuzz10
    Avatar
    Community Member
    786 Posts

    Re: Problems with HTTPRequest::send_file(), header issue Link to this post

    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 ...

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Problems with HTTPRequest::send_file(), header issue Link to this post

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

  • Aaron Brockhurst
    Avatar
    Community Member
    30 Posts

    Re: Problems with HTTPRequest::send_file(), header issue Link to this post

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

    Aaron

    2435 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.