Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Can I halt execution of a Controller_Template based class?
  • I'm building a download controller that reads a file from a location on the server, then passes that file along to the user after adding a few headers (at least that's what it's supposed to do). The controller extends my Base class, which itself is an extension of Controller_Template. Is there any acceptable method to halt execution of the app without rendering a template view? Or should I be rendering an empty view maybe? My downloader code looks like this:

    public function action_download($id = null)
    {
    is_null($id) and \Response::redirect('/resources/files');

    // specifying the client and category IDs prevents unauthorized access
    $file = File::find($id, array('where' => array(
    // @todo this is broken when a superuser uploads a file
    // array('category_id', 'IN', $this->current_user->categories['flat']['all']),
    array('client_id', $this->current_client->id),
    )));

    // @todo verify that the record exists
    $fp = fopen($file->path, 'rb');
    header('Content-type: ' . $file->type);
    header('Content-length: ' . $file->size);
    header('Content-disposition: attachment; filename="' . $file->name . '"');
    fpassthru($fp);
    exit;
    }

    This doesn't work. It results in an ERR_EMPTY_RESPONSE. And I'm sure that I shouldn't be using exit.
  • Why so complex, and not use File::download() ?

    Are you using output encoding (enabled by default in production)? You need to switch that off if you're downloading files.
  • Hah! I didn't realize there was a File class. It works great in my dev environment. What will I have to change for production? Do I have to globally disable output encoding or can I selectively bypass it?
  • HarroHarro
    Accepted Answer
    If you use File::download(), nothing. It will disable output compression for you if it is enabled.

    It will also wait with starting the download until after Fuel has finished execution, to avoid shutdown events messing up your download in case they produce output (like an error page for example).

    You don't have to call exit or die after it, just a call to File::download() will suffice.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion