CodeIgniter Forums
How to access images under writable folder - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: How to access images under writable folder (/showthread.php?tid=77226)

Pages: 1 2


RE: How to access images under writable folder - ignitedcms - 03-30-2022

I like the random hash idea, seems easier to maintain.


RE: How to access images under writable folder - badger - 03-30-2022

rather than one folder with gazillions of images (you'll always have way more than you expect) i have an images folder with 20 subfolders into which the images are allocated to maintain an approx even distribution


RE: How to access images under writable folder - John_Betong - 03-30-2022

Quote:@MGatner:
I’m a fan of symlinks (assuming you aren’t on Windows). Just create a symlink in public to the uploads folder. This also helps you keep your folder permissions consistent. Obligatory warning to use good security when making uploaded files accessible.

I’m using a Linux server, not familiar with using symlinks and would be grateful for a detailed example:

Code:
echo ‘<img src=‘“ .WRITABLE .’/folder/img.jpg” .’” alt=“#”>’;



RE: How to access images under writable folder - ignitedcms - 03-30-2022

(03-30-2022, 02:07 PM)badger Wrote: rather than one folder with gazillions of images (you'll always have way more than you expect) i have an images folder with 20 subfolders into which the images are allocated to maintain an approx even distribution

Yes I agree it is a good idea to have a two step access or more, such as folder (maybe user's id / timestamp) + random file name.

I think you have to be careful with just relying on random string names because of the 'Birthday Paradox' and chances of collision.

This is also why GUID's are probably the preferred method with maybe even appending or pre-appending a timestamp


RE: How to access images under writable folder - dgvirtual - 09-16-2023

This might be useful to some, so here is how to properly keep image files (or other files) not in public dir but in writable dir, still loadable in html (but also, if needed, restricting public access to them):


1. Place image files in writable/uploads/img

(e.g., I have there an image named profile-9.jpg and another image placeholder.jpg )


2. Make a special controller method to access those files. Here is mine (controller class name, lets say, is Files):
PHP Code:
    public function profile(int $id) {
        $path WRITEPATH 'uploads/img/';
        $name 'profile-' $id '.jpg';

        if (!file_exists($path $name)) {
            $name 'placeholder.jpg';
        }
        return $this->response->download($path $namenull);
    

(more on the usage of the response::download method - here in the docs)


3. Use the route /files/profile/9 to access the image in the html src property, for example like this:

Code:
<img src="<?=base_url('files/profile/' . $id) ?>" />
The added bonus in my case is that, if the expected image is not there, the placeholder image is loaded instead. But that is a matter of implementation.