Reputation: 286
I have an application in PHP 7 using the Slim3 microframework.
The structure of the project is as follows:
To run my application I use this configuration in apache:
<VirtualHost *: 80>
ServerName app
DocumentRoot "C: \ xampp \ htdocs \ app \ public"
</VirtualHost>
To access the public assets (which are inside the public folder) I use these routes:
href = "../ assets / images /"
href = "../ assets / scripts /"
href = "../ assets / styles /"
I have the problem when I want to access the * uploads * files.
Since, I can't find how to put the path in the href attribute.
Perform the following tests:
href =" ../ assets /file.jpg "
href =" ../file.jpg "
href =" ../../file.jpg "
The question is: How can I access the files that are in the Uploads folder?
Note1: Some people recommended that I put the Uploads folder inside public
Note2: Inside public/index.php i have this code
$root = dirname(__DIR__);
$settings['root'] = $root;
$settings['temp'] = $settings['root'] . '/temp';
$settings['public'] = $settings['root'] . '/public';
$settings['uploads'] = $settings['root'] . '/uploads';
Note3: I try using a path url like this
<img src="{{ uploads }}/product_1.png">
But get this error on console:
Not allowed to load local resource
Upvotes: 0
Views: 470
Reputation: 95
First of all every public file should land in your public dir (which should by allowed by apache to be public) Rest of dirs should be disallowed from external browsing.
And then you won't have any problem because public dir is allowed to browsing in your apache config file:
<VirtualHost *: 80>
ServerName app
DocumentRoot "C: \ xampp \ htdocs \ app \ public"
</VirtualHost>
Upvotes: 1
Reputation: 463
Right now you're using relative paths (../file.jpg
). This is a tricky way of doing things, and isn't generally good practice. Instead, consider defining a variable that points to $_SERVER['DOCUMENT_ROOT'].
define('SITE_ROOT', $_SERVER['DOCUMENT_ROOT']);
echo '<img href="' . SITE_ROOT . '/public/assets">';
Upvotes: 1