Point to a folder before DOCUMENT ROOT?

3

Below is the structure of the folders:

/home/web/site/public_html

If I give echo $_SERVER['DOCUMENT_ROOT']

I'm going to get the path to public_html, but I'd like to leave some inaccessible files directly through the URL so I'm putting those files within /home/web/site but I'd like to not have to use ../../../ to go back up to /site and having to guess how many folders I have to get back to the destination, is there any easier way to point to a folder before the default?

And if I want to call the files to a img or video tag through src="algo..." would it be a more practical solution to serve such files through a php script and limit the permissions of it? If so, how can I create it?

    
asked by anonymous 19.01.2017 / 13:41

2 answers

2

The server would not "point" to the directory, after all, it would not be public. But you can upload private content to a PHP script. Define a constant and make a kind of "access validation" in the application itself.

How can you do this with images using the php-gd library:

image.php :

<?php
defined('FILES') OR define('FILES', '/home/web/site/');
if($_GET['img']){
    $filename = FILES.$_GET['img'];
    if(file_exists($filename)){
        $file_info = getimagesize($filename);
        foreach ($file_info as $i => $v){
            if($v == 'image/png'){
                $mime = $v;
                $img = imagecreatefrompng($filename);
            }
            elseif($v == 'image/jpg'){
                $mime = $v;
                $img = imagecreatefromjpg($filename);
            }
        }
        header("Content-type: " . $mime);
        imagepng($img);
        imagedestroy($im);
    }
    else{
        echo 'Imagem nao localizada!';
    }
}
else{
    echo 'Imagem não definida!';
}

Simply pass the full name of the image in the url: imagem.php?img=imagename.jpg .

To stream videos, it pays to study things like VideoStream.php . See:

stream.php

<?php
include 'VideoStream.php';
defined('FILES') OR define('FILES', '/home/web/site/');
$video = new VideoStream(FILES.'videoname.mp4');
$video->start();

You can set the security and proper permissions on PHP and it loads content to the browser.

    
19.01.2017 / 14:12
2

A simple way is to set a constant for the location you want.

define('PRIVATE_BASE', _DIR_.'/../');

With this, just call PRIVATE_BASE to mount the base of the path.

Of course this will depend on how the application is structured. Usually it should have a single file that serves as a route to all others. The index.php , for example.

If you do not have a route file, you can choose a "config.php" or something like that. Then include this "config.php" in the files you want to get path information.

Note that you are still using directory indentation. But you will only use it once.

It is preferable to use the indentation if you want to give the application flexibility.

If you still want to set the path statically, be aware that if you need to run the application in an environment with different structures, you must manually edit the paths.

define('PRIVATE_BASE', '/home/www/site/');

    
19.01.2017 / 14:26