0

I have an application in PHP 7 using the Slim3 microframework.

The structure of the project is as follows:

Projecct Structure

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:

  • copying and pasting a .jpg file inside assets. Accessed with href =" ../ assets /file.jpg "
  • copying and pasting a .jpg file into public. Accessed with href =" ../file.jpg "
  • copying and pasting a .jpg file outside of public. Not accessible with 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

Dev 200
  • 246
  • 1
  • 6
  • 12

2 Answers2

1

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">';
Kaboodleschmitt
  • 453
  • 3
  • 13
  • Can you not simply use ``? – Kaboodleschmitt Aug 21 '20 at 19:47
  • Without seeing more of your code it's tough... You shouldn't be trying to access about:blank, though? If you can't access the contents of your uploads directory it could well be a directory or [file permissions](https://www.guru99.com/file-permissions.html) issue (maybe the image isn't set to allow execution or something) **Edit** seeing this: `` giving 'not allowed' makes it more likely it's a permissions error – Kaboodleschmitt Aug 21 '20 at 19:55
1

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> 
pjplonka
  • 95
  • 8