0

I have the following link on my website - http://mywebsite/multimedia/pronounciation/265.mp3 which gets me the file bypassing controllers. But I would like to log request and then return this file. So I created controller which logs request and then reroutes to the file:

class Controller_GetSound extends Controller {

    public function action_index() {
        Request::factory('multimedia/pronounciation/265.mp3')
                ->method(Request::POST)
                ->post($this->request->post())
                ->execute();
    }
}

But it doesn't work as expected. How can I return resource file from controller?

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488

2 Answers2

2

Kohana has a send_file function. That function does a good job on splitting large files and sending correct mime types.

@see http://kohanaframework.org/3.3/guide-api/Response#send_file

Your code should be:

class Controller_GetSound extends Controller {

    public function action_index() {

        $this->response->send_file('multimedia/pronounciation/265.mp3',TRUE,array(
            'mime_type' => 'audio/mpeg',
        ))
    }
}

You actually don't really need to set the mime_type. Kohana will find the correct mime_type for your.

Matthias Lill
  • 318
  • 2
  • 6
  • Thank you! Have you ever used it? – Max Koretskyi Dec 21 '13 at 07:17
  • Yes, i use it to serve user restricted files. Like checking if a user is allowed to download a file. It also supports download "resumes". And if you check the code of that function you can see that there is much more going on than just the mime type. – Matthias Lill Dec 21 '13 at 09:05
1

It sounds like you want to implement something known as X-Sendfile. I think?

The controller would look something like this:

class Controller_GetSound extends Controller {

    public function action_index() {
        $this->response->headers(array(
            'Content-Type' => 'audio/mpeg'
            'X-Sendfile' => 'multimedia/pronounciation/265.mp3',
        );
    }
}
Darsstar
  • 1,885
  • 1
  • 14
  • 14
  • Thanks! S you are saying I'll probably need to download and install this X-Sendfile mod for Apache? Have you ever used it before? – Max Koretskyi Dec 13 '13 at 05:48
  • No, I haven't ever used it. But I would try it if I had a similar need. The usage does not look hard at all, which is why I made a mental note when I saw it mentioned on the Kohana forums. (Probably over a year ago, which is why I remembered the feature and had to google the name.) – Darsstar Dec 13 '13 at 07:11