6

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.

3 Answers 3

5

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"
7
  • So you can do this anywhere.... and it would work? e.g. <?php include "../i-modules.php"; ?>
    – Arthor
    Commented Feb 3, 2012 at 13:26
  • @Arthor, this isn't necessary unless you really need it, you can reference files relative or absolute. The $_SERVER method adds a prefix to your relative path and makes it absolute.
    – Jakub
    Commented Feb 3, 2012 at 13:28
  • @thenetimp - I am going to try that and also, welcome to stackoverflow.
    – Arthor
    Commented Feb 3, 2012 at 13:29
  • Yes you can do it anywhere. Includes, requires, it's all linking to the absolute path of the file it would look something like. include($_SERVER['DOCUMENT_ROOT'] . '/includes/myincludedfile.php');
    – thenetimp
    Commented Feb 3, 2012 at 13:30
  • @Jakub - Great. I am going to give it a go and try it. I will play around and report back. Thank you
    – Arthor
    Commented Feb 3, 2012 at 13:30
1

Arthor, there is no correct way, you are correct it is relative vs absolute reference to a file/url/resource.

There is no wrong way. However each approach has its upsides, and downsides:

Key difference points:

  • relative: portability (you can move your app by copy & paste somewhere else, as you call URLS ../array/file.ext you will always refer to them correctly.
  • relative: moving a file that makes reference to a resource relatively means you have to update its reference, if file was in /file/folder/stuff/file.ext and you move it, you need to update the reference to your /array/file.ext then.
  • absolute: less portable, unless you use it in a URL sense (include javascript / images, etc)
  • absolute: moving files that reference other files, means you don't have to change their code.
  • and many more...

Personally I prefer absolute, but really it depends on your reasoning, neither of which is wrong.

Oh and to clarify (if you didn't know) ../ simply means "go down one directory, and then look from there, it is used in a relative link, where you outline where the file is relative to your script that is calling it.

2
  • But, can you not make the first part of the URL as a variable so you only need to change that in the future.
    – Arthor
    Commented Feb 3, 2012 at 13:28
  • @Arthor, if you make it a variable, it will effectively be absolute, but yes, you can do it, programming allows you to do so, its not right or wrong.
    – Jakub
    Commented Feb 3, 2012 at 13:29
1

If you don't use the DOCUMENT_ROOT it depends on your folder structure. ".." means that you go one folder back.

If you have this structure:

root_dir
|
folderA
|  |
|  - A.php
|
folderB
   |
   - B.php

If you are in file B.php and want to include A.php you have to use this path:

inlcude "../folderA/A.php";

so you go one folder back, then you are in the root_dir from where you can access A.php via folderA/A.php.

6
  • so it is better to use DOCUMNET_ROOTas a better means of coding.
    – Arthor
    Commented Feb 3, 2012 at 13:31
  • I would use the DOCUMNET_ROOT variant, so you don't have to care about the relative path.
    – tbraun89
    Commented Feb 3, 2012 at 13:34
  • Your way of explaining my problem is great!!! It seems that DOCUMNET_ROOT is the way to go. I agree there is NO ONE correct way but there are good codes or practices. Thus, this is one.
    – Arthor
    Commented Feb 3, 2012 at 13:35
  • 1
    Yes, you can do both, nothing is right or wrong, but imo using the DOCUMENT_ROOT is better readable and you can avoid messing around with with the relative paths, because this is getting more complex if you have many includes or if you include files that are included in others.
    – tbraun89
    Commented Feb 3, 2012 at 13:39
  • 1
    I don't think this will effect the performance, you can read more about the $_SERVER array here and other information you can obtain.
    – tbraun89
    Commented Feb 3, 2012 at 15:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.