0

I want to display images on my website( based on php, runs on an apache ), but they are not in a publicly accessible directory, but outside the root directory.

This is how the folder structure looks like:

inc/
public/ <- accessible via webdomain
vault/
     /media/
           /2021/ <- images are located here

In the folder public is my index.php, here the image should then be output.

My first idea would be to write a php-file, which outputs the content accordingly. It would also be important to me that the actual path of the images is not public.

How would you go about it? What would be the clearest approach?

John Conde
  • 217,595
  • 99
  • 455
  • 496
kirator
  • 1
  • 1

1 Answers1

0

with the magic constant __DIR__ you will get the full path the current PHP-script you are running. But note: if you include another PHP-file from another directory (/subdir/script.php for example) and request __DIR__ in there, you get the full path to '/subdir'. Use '../' for pointing 1 dir above, '../../' for 2 dir's above and so on.
See: How to use __dir__?

So if your index.php file (accessed by browser via domain) is inside /inc/public/

$path = __DIR__; // would contain '/rootdir/inc/public'
$pathToImages = $path . '/../../vault/media/2021/';
$imgContent = file_get_contents( $pathToImages . 'imagename.jpg' );

You could make a subdomain like media.example.com and link this to another PHP-script that handles your media-content like images. So you can use <img src="https://media.example.com/?img=sample" />

Atomkind
  • 341
  • 2
  • 11