Niel Sinel
Niel Sinel

Reputation: 291

PHP - How can I display an image coming from the different directory without using the absolute path?

I just wanna ask how can I display an image from a different directory without using absolute path (cause it's not working). Let's take this example:

Lets assume that I have an htdocs folder and I have a folder inside it named system, the system folder contains 2 folders and 1 navigations.php file inside. The first folder named as images and it is the storage of the images that would be use in the program, while the second folder named as files and it contains 2 different folders, the phpfiles and cssfiles, so inside the phpfiles folder I have a PHP file named loans.php that would include the navigations.php (I dont have a problem in including the file) in the navigations.php I have a code wherein it will retrieve some images on the images folder, but when I included it in the loans.php the images in the navigations.php did not load. I know that there is are issues with the directories because the file loans.php located at the sub-directories of the system folder. I tried using absolute path (C:/xampp/htdocs/system/images/image.png) but It's not working, so an alternative I used <base> tag to use it in a relative path but still it is not appearing in the loans.php cause it's located on the sub-directories of the system folder. What's the best way to fix it?

PS: Nevermind the example, just focus on the situation :)

-ADDITION (Problem Solved)

As a solution I created a PHP function that prints a string depending on a number of step backs on a directory to access directories from a different sub directories.

function numberOfBack($num){
    $strback = "";
    if ($num == 0){
        return $strback;
    } else {
        for ($x = 1; $x <= $num; $x++){
            $strback.="../";
        }
        return $strback;
    }
}

To use this, the string that would be return will be concatenated to the src of the image or PHP file that I'm going to retrieve. Thank you for the hints and answers.

Upvotes: 0

Views: 320

Answers (2)

Spade
Spade

Reputation: 591

Based on your comment replying to mine your absolute path should be as followed:

http://localhost/system/images/image.png

I am not an xampp expert but I believe it is configured to have apache render that address instead of the actual folder path like c:/htdocs/system/

Upvotes: 1

TurdPile
TurdPile

Reputation: 1036

If you are only accessing navigations.php via loans.php the solution is to access the relative path of the image from the perspective of loans.php.

In this case, based on your description, it seems that the relative path would be: ../images/image.png.

Upvotes: 1

Related Questions