Hack Forums
[PHP] How do I point to the root web directory in this code? - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Web Development (https://hackforums.net/forumdisplay.php?fid=183)
+---- Thread: [PHP] How do I point to the root web directory in this code? (/showthread.php?tid=5962653)



How do I point to the root web directory in this code? - Babadook - Apr 21, 2019

I need this code set to where it starts from the root directory of my site. I use this both locally and externally so I need it to work for both.

I've tried all of these:
PHP Code

<?php 
$dir 
= $_SERVER['DOCUMENT_ROOT'];
$dir2 = $_SERVER['HTTP_HOST'];
/
uploads/Photos/MovieCovers/*.{jpeg,jpg,png,gif}
./uploads/Photos/MovieCovers/*.{jpeg,jpg,png,gif}
../uploads/Photos/MovieCovers/*.{jpeg,jpg,png,gif} 

What works:
PHP Code

<?php 
$images 
= glob('../../../../uploads/Photos/MovieCovers/*.{jpeg,jpg,png,gif}', GLOB_BRACE); 

However, I can't use this as the file is used in multiple directories with differing lengths from the root.

I can set it to use the files in the local directory just fine. But I can't get it above the directory. When I do this with $dir it breaks the image and doesn't display "Temporary.jpg" while trying to grab "mysite.com/var/www/html/uploads/Photos/MovieCovers/5th%20Passenger%20(2018)@2x.jpg". $dir2 and everything else shows temporary image and


Specific line i want pointed to '/uploads/Photos/MovieCovers/*.{jpeg,jpg,png,gif}':
PHP Code

<?php 
$images 
= glob($dir . "/uploads/Photos/MovieCovers/*.{jpeg,jpg,png,gif}", GLOB_BRACE); 

Full Code:
PHP Code

<?php
$dir 
= $_SERVER['DOCUMENT_ROOT'];
$temp = '/images/Temporary.jpg';

$movies = glob('./*.{mp4,m4v}', GLOB_BRACE);
$images = glob($dir . "/uploads/Photos/MovieCovers/*.{jpeg,jpg,png,gif}", GLOB_BRACE);
$subtitles = glob('./*.{srt}', GLOB_BRACE); #To be used later.

echo file_get_contents($dir . "/html/header.html");

 
   foreach ($images as $image) {
 
       $lookup[pathinfo($image, PATHINFO_FILENAME)] = $image;
 
   }

 
   foreach ($movies as $movie) {
 
       $image = $lookup[pathinfo($movie, PATHINFO_FILENAME)] ?? $temp;
 
       echo '<a href="' . $movie . '">
                    <img src="' 
. $image . '" style="width:300px;height:350px;border:0;">
                </a>'
;
 
   }
echo 
file_get_contents($dir . "/html/footer.html");
?>



RE: How do I point to the root web directory in this code? - Disease - Apr 21, 2019

(Apr 21, 2019 02:27 PM)Babadook Wrote:  I need this code set to where it starts from the root directory of my site. I use this both locally and externally so I need it to work for both.

I've tried all of these:
PHP Code

<?php 
$dir 
= $_SERVER['DOCUMENT_ROOT'];
$dir2 = $_SERVER['HTTP_HOST'];
/
uploads/Photos/MovieCovers/*.{jpeg,jpg,png,gif}
./uploads/Photos/MovieCovers/*.{jpeg,jpg,png,gif}
../uploads/Photos/MovieCovers/*.{jpeg,jpg,png,gif} 

What works:
PHP Code

<?php 
$images 
= glob('../../../../uploads/Photos/MovieCovers/*.{jpeg,jpg,png,gif}', GLOB_BRACE); 

However, I can't use this as the file is used in multiple directories with differing lengths from the root.

I can set it to use the files in the local directory just fine. But I can't get it above the directory. When I do this with $dir it breaks the image and doesn't display "Temporary.jpg" while trying to grab "mysite.com/var/www/html/uploads/Photos/MovieCovers/5th%20Passenger%20(2018)@2x.jpg". $dir2 and everything else shows temporary image and


Specific line i want pointed to '/uploads/Photos/MovieCovers/*.{jpeg,jpg,png,gif}':
PHP Code

<?php 
$images 
= glob($dir . "/uploads/Photos/MovieCovers/*.{jpeg,jpg,png,gif}", GLOB_BRACE); 

Full Code:
PHP Code

<?php
$dir 
= $_SERVER['DOCUMENT_ROOT'];
$temp = '/images/Temporary.jpg';

$movies = glob('./*.{mp4,m4v}', GLOB_BRACE);
$images = glob($dir . "/uploads/Photos/MovieCovers/*.{jpeg,jpg,png,gif}", GLOB_BRACE);
$subtitles = glob('./*.{srt}', GLOB_BRACE); #To be used later.

echo file_get_contents($dir . "/html/header.html");

    foreach (
$images as $image) {
        
$lookup[pathinfo($image, PATHINFO_FILENAME)] = $image;
    }

    foreach (
$movies as $movie) {
        
$image = $lookup[pathinfo($movie, PATHINFO_FILENAME)] ?? $temp;
        echo 
'<a href="' . $movie . '">
                    <img src="' 
. $image . '" style="width:300px;height:350px;border:0;">
                </a>'
;
    }
echo 
file_get_contents($dir . "/html/footer.html");
?>

Typically you'd define this as a constant, or via a configuration library/object, during instantiation of your application. In other words, you might define this via index.php, before requiring in relevant files as a result of the web request.

PHP Code

<?php 
define
("ROOT", __DIR__ . "/"); 

Alternatively, you could define this as an environment variable on the machine itself (supposing you have access to do that). You can then retrieve that during execution via the getenv() function.

PHP Code

<?php 
getenv
("ROOT"); 



RE: How do I point to the root web directory in this code? - Babadook - Apr 21, 2019

(Apr 21, 2019 03:29 PM)Disease Wrote:  Typically you'd define this as a constant, or via a configuration library/object, during instantiation of your application.  In other words, you might define this via index.php, before requiring in relevant files as a result of the web request.

PHP Code

<?php 
define
("ROOT", __DIR__ . "/"); 

Alternatively, you could define this as an environment variable on the machine itself (supposing you have access to do that).  You can then retrieve that during execution via the getenv() function.

PHP Code

<?php 
getenv
("ROOT"); 

Thank you. I'll look into everything after work tomorrow!


RE: How do I point to the root web directory in this code? - .trigious - Apr 23, 2019

Don't use HTTP_HOST as directory source. It can be controlled by attackers and can lead to path traversal attacks if you are not on a shared hosting environment. :)