Serving Images to get a request

前端 未结 2 1228
故里飘歌
故里飘歌 2021-01-26 05:19

I am using CodeIgniter rest Controller. I want to be able to serve images on GET requests made by the client.

Is this the best option or should i just provide a link to

2条回答
  •  春和景丽
    2021-01-26 05:46

    To make CodeIgniter set the proper headers and such you could do this in your controller:

    $this->output->set_content_type('jpeg')->set_output(file_get_contents('path_to_file'));
    

    And it will output (as request response) the image content as a file. No view required.

    Please note though that this is a bit of extra overhead as the file is processed by PHP instead of just the webserver (Apache/Nginx). It only makes sense if you need to have some business logic on the request, such as logging or authorization (though even that can be done without PHP). If you are just outputing the image it is therefor better to link straight to file and leave PHP out of it.

提交回复
热议问题