Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

PHP 

How do I point to the root web directory in this code?

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");
?>
I collect tutorials/methods. I enjoy learning the methods more than using them. Feel free to DM them to me!

I often forget HF exists, I check in every now and then.
(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"); 
<Disease />
(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!
I collect tutorials/methods. I enjoy learning the methods more than using them. Feel free to DM them to me!

I often forget HF exists, I check in every now and then.
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. :)


Possibly Related Threads...
Thread / Author Replies Views Last Post
[PHP] by Dale CarnegieMay 15, 2018 11:53 PM 5 156 May 15, 2018 11:53 PM
Last Post: Squito