![]() |
Hi,
CodeIgniter 4 has the DownloadResponse class that presents a dialog box in the browser to download the file. However, I have images in a folder outside the public directory. And I would like to let the browser directly display (not download) the image after a successful authorization. Since the images are in a private folder, an image URL can't be shared. I'm able to successfully return the download response from the controller that prompts a dialog box in the browser to save the image. However, I don't know if there is a way to have a CodeIgniter controller return an image so that the browser could straight display it? If I cancel the dialog box, the page remains empty All the examples I searched on the internet are about using readfile() but I'm not sure how do I use it either with a HTTP response or download response. URL: https://www.localwebsite.test/attachment/1.webp attachment is the name of the controller So how do I return image to the browser from private folder for display purpose? PHP Code: return $this->response->download(ROOTPATH . 'private/1.webp', null, true)->send(); ![]()
1. You don't need to call the send() method. At least if you don't know how it works and what consequences it will cause.
2. The solution to your case will be in version 4.4. 3. To display the file in the browser, the header "Content-Disposition: inline" is needed. But in the current implementation, this header will be overwritten. The easiest TEMPORARY solution for you would be... 1. Take the DownloadResponse class from the repository from the 4.4 branch (https://github.com/codeigniter4/CodeIgni...sponse.php) 2. Save the file in the application directory app/DownloadResponse.php 3. In the file, change the namespace from "CodeIgniter\HTTP" to "App". 4. Next, in the controller method, call the following code. PHP Code: (new \App\DownloadResponse('1.webp', true))->setFilePath(ROOTPATH . 'private/1.webp')->inline()->send(); I haven't tested it, but in theory it should work. When you upgrade to version 4.4, use the ready-made solution described in the documentation. And without calling send() and exit().
Thank you for the response.
I have discovered the `Content-Disposition` header and solved it yesterday using existing version as follow: Code: $download = $this->response->download('filename.jpg', 'filepath', true); Yet 4.4 version is a fair one. |
Welcome Guest, Not a member yet? Register Sign In |