CodeIgniter Forums
Is there a way to create a symbolic link within the public folder? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Is there a way to create a symbolic link within the public folder? (/showthread.php?tid=91262)



Is there a way to create a symbolic link within the public folder? - kcs - 07-11-2024

Hi,
I am working on an application that will enable admin users to upload images to be used inside posts. Is there a way to tell the application that for example the public/images folder is not inside the public folder but let's say /home/user/myproject/sharedassets/images which is the server's path (the codeigniter application being inside the folder myproject) ? 
Thanks for your help


RE: Is there a way to create a symbolic link within the public folder? - captain-sensible - 07-11-2024

seems a complicated approach , with capacity for broken URL .

First to display any image in any post all you need is the image name and where it is.

Im my blog article I get image displayed using

Code:
<img src =".base_url('blogImages')."/".esc($blog['image']).">

that code is in a view that will display a blog article
Code:
$blog['image'];
// the above gets just the name of an image wich is stored in a field in a database
// i.e  "someimage.jpg" is stored  on the basis of text, in a field in a databse , alomg with blog article, id etc

all blog images are stored in a directory called blogImages. so i use constant base_url() and that directory name to reference image

Code:
base_url('blogImages')
//base_url() eqautes to public directory, so blogImages is a direcvtory in public

ways of getting stuff dfrom reuest have chnagedf i think but here is a legacy



Code:
$this->imageFile = $this->request->getFile('userfile');
$this->imageFileName = $this->imageFile->getName();
//thr above if memory serves me well gets image foole name ; once known the name can be put in a databse


step to shift image to where i want
Code:
     $this->imageFile->move('blogImages',    $this->imageSlug );
$this-imageSlug, is the name i want to give to image , after any white space in image name has been removed and bad chars
maybe if you had a directory : sharedassets/images at root of web app at least if it was moved all should be Ok with relative paths
then :
Code:
$this->imageFile->move(ROOTPATH.'sharedassets/images ',    'name you want ot give image ');



RE: Is there a way to create a symbolic link within the public folder? - kcs - 07-12-2024

The reason I am looking for a solution is so I can make upgrades on the application without risking to loose uploaded files. I used this structure with rails applications in the past, all assets remaining stored independently. That was very flexible for deploying new app releases.

While searching, it looks like what might answer my needs is to use the publisher library https://codeigniter.com/user_guide/libraries/publisher.html .

Looking at the examples, it feels like what this does basically is to copy files from a folder into the public one, so that duplicates files on the server side. Am I correct? Would there be a solution to not duplicate the files?


RE: Is there a way to create a symbolic link within the public folder? - ozornick - 07-12-2024

You cannot make files not in public/ accessible from the browser.
Alternatively, you should write your own handler to read the file and send a Response (https://www.php.net/manual/en/function.readfile.php).
But this will cause an additional load on the disk.


RE: Is there a way to create a symbolic link within the public folder? - kcs - 07-14-2024

Thanks for confirming the impossibility. I will try to use the publisher library to see if I manage to do something with it (if anyone has a example I can look at in addition to those in the documentation, that'd be super helpful).


RE: Is there a way to create a symbolic link within the public folder? - ozornick - 07-14-2024

Simple copy-paste files
https://github.com/neznaika0/codeigniter-expenses/tree/main/app/Publishers


RE: Is there a way to create a symbolic link within the public folder? - kenjis - 07-15-2024

Why don't you just create a symlink in the public folder?


Code:
$ cd public/
$ ln -s ../sharedassets/images .



RE: Is there a way to create a symbolic link within the public folder? - kcs - 08-01-2024

@kenjis I tried but files do not appear in the browser. Thanks @ozornick  I will try following your example (that expenses app you made is pretty useful).