Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6376863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:47:55+00:00 2026-05-25T01:47:55+00:00

I am writing an action in a controller where in a certain case, I

  • 0

I am writing an action in a controller where in a certain case, I want to output raw image data directly, and want to set the header content-type appropriate. However I think the header is already being set earlier by CakePHP (I am setting render to be false).

Is there a way to get around this? Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T01:47:56+00:00Added an answer on May 25, 2026 at 1:47 am

    As said before, CakePHP does not send headers when render is false. Beware though, that any code doing an ‘echo’ will send headers (except you are using output-buffering). This includes messages from PHP (warnings etc.).

    Sending the file can be done in numerous ways, but there are two basic ways:

    Send the file using plain PHP

    function send_file_using_plain_php($filename) {
        // Avoids hard to understand error-messages
        if (!file_exists($filename)) {
            throw RuntimeException("File $filename not found");
        }
    
        $fileinfo = new finfo(FILEINFO_MIME);
        $mime_type = $fileinfo->file($filename); 
        // The function above also returns the charset, if you don't want that:
        $mime_type = reset(explode(";", $mime_type));
        // gets last element of an array
    
        header("Content-Type: $mime_type");
        header("Content-Length: ".filesize($filename));
        readfile($filename);
    }
    

    Use X-Sendfile and have the Webserver serve the file

    // This was only tested with nginx
    
    function send_file_using_x_sendfile($filename) {
        // Avoids hard to understand error-messages
        if (!file_exists($filename)) {
            throw RuntimeException("File $filename not found");
        }
    
        $fileinfo = new finfo(FILEINFO_MIME);
        $mime_type = $fileinfo->file($filename); 
        // The function above also returns the charset, if you don't want that:
        $mime_type = reset(explode(";", $mime_type));
        // gets last element of an array
    
        header("Content-Type: $mime_type");
        // The slash makes it absolute (to the document root of your server)
        // For apache and lighttp use:
        header("X-Sendfile: /$filename");
        // or for nginx: header("X-Accel-Redirect: /$filename");
    
    }
    

    The first function occupies one PHP-process / thread while the data is being send and supports no Range-Requests or other advanced HTTP-features. This should therefore only be used with small files, or on very small sites.

    Using X-Sendfile you get all that, but you need to know which webserver is running and maybe even a change to the configuration is needed. Especially when using lighttp or nginx this really pays off performance-wise, because these webservers are extremly good at serving static files from disk.

    Both functions support files not in the document-root of the webserver. In nginx there are so called “internal locations” (http://wiki.nginx.org/HttpCoreModule#internal). These can be used with the X-Accel-Redirect-Header. Even rate-throtteling is possible, have a look at http://wiki.nginx.org/XSendfile.

    If you use apache, there is mod_xsendfile, which implements the feature needed by the second function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm always writing tests to check my controller restricts people from certain actions depending
I am writing an excel report using an action,controller, servlet struts framework. The report
I'm writing some unit tests to assert that all our controller action methods are
I often find myself writing this: params.delete(:controller) params.delete(:action) params.delete(:other_key) redirect_to my_path(params) The trail of
I want to handle exceptions thrown by any controller action with an ErrorView model
Is there a simpler way of writing this: map.old_site_cusom_packages '/customs_packages_options.html', :controller => :public, :action
I'm writing a spec for my Rails controller, this is the action I'm testing:
I am writing a server, and I send each action of into a separate
Writing some test scripts in IronPython, I want to verify whether a window is
Writing an asynchronous Ping using Raw Sockets in F#, to enable parallel requests using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.