get file like pics and media through controller

159 views Asked by At

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?

2

There are 2 answers

2
Matthias Lill On BEST ANSWER

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.

2
Darsstar On

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',
        );
    }
}