Search code examples
phpcoding-stylemenu

Reference to file regardless of the folder structure and depth


My research has lead me to believe that it might be called, absolute URL / relative URL. But please, I am not sure. So this is not reason to give me a minus as I have just reached 50 and I am on a STEEP learning curve.

In my index.php

I have a reference to a csv array. /array/test.csv

Then, I have a folder called MENU. In this folder I have a PHP called menu.php which also needs to make reference to the test.csv. However I Cannot put /array/test.csv, instead I have to put ../array/test.csv.

So depending where you are in a folder structure, you might have to use / or ../ or ../../,

Can someone point out the correct way to do this as I know that I will have problem if I do not learn the correct way.


Solution

  • I would call the full path of the file.

    $_SERVER['DOCUMENT_ROOT'] . "/array/test.csv"
    

    The beginning says where the htdocs folder for your web server is. Then we add the rest of the path from there.

    So to store the path in a variable you may do.

    $csvFile = $_SERVER['DOCUMENT_ROOT'] . "/array/test.csv"