CodeIgniter Forums
Getting path/URL of uploaded files - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Getting path/URL of uploaded files (/showthread.php?tid=76848)



Getting path/URL of uploaded files - dariusbaker - 06-26-2020

Hey guys, I hope you're doing alright in this pandemic.

I've searched the forums and StackOverflow but I can't seem to find solutions to what I believe is a very simple concept: is there a method that returns the full path/URL of the uploaded file?

Here's what my flow looks like:
1. Image is uploaded via a form.
2. Image is moved using '$file->store()'
3. The value returned is inserted into the database (e.g. "20200626/1593171870_2ebce4905386e30800cf.jpeg")

I'm unable to build a path/URL that can display the image in another controller. I've tried 'realpath()' in combination with 'APPPATH', 'WRITEPATH', and the value attained in step 3 but I'm given 404 errors. An example:
Code:
http://localhost:8080/Users/path/to/app/writable/uploads/20200626/1593171870_2ebce4905386e30800cf.jpeg
 I guess the error makes sense because it points to the system path.

So, how does one point to the path/URL of an uploaded file in CI?

Thanks for taking time to read this.


RE: Getting path/URL of uploaded files - lokeshk - 06-27-2020

In view you will call this code after image uploaded successfully.

echo $image_data['full_path'];


RE: Getting path/URL of uploaded files - dave friend - 06-27-2020

(06-26-2020, 06:34 PM)dariusbaker Wrote:
Code:
http://localhost:8080/Users/path/to/app/writable/uploads/20200626/1593171870_2ebce4905386e30800cf.jpeg
 I guess the error makes sense because it points to the system path.

So, how does one point to the path/URL of an uploaded file in CI?

Seems like you already know the answer but you cannot access assets in /app/writable/uploads using HTTP because that folder is outside the webroot. If you want to display the image using some HTML tag then you will have to move (or copy) the image into the webroot somewhere, i.e. /public/assets/images/20200626/somefile.jpg.

The absolute path to the file would be

PHP Code:
$imagePath WRITEPATH 'uploads/' $imageLocation

Assumes $imageLocation comes from the database and includes the date folder and the file's full name.