user1463822
user1463822

Reputation: 887

PHP include "../" vs "/../"

what's the difference between

require("../classes/M8.php");

and

require("/../classes/H8.php");

How will include path will differ?

Upvotes: 5

Views: 184

Answers (2)

Jelle Ferwerda
Jelle Ferwerda

Reputation: 1229

/.. is a path relative the root of the server.
../ is a path relative to the current script folder.

I see a request for more information is made.

If you are in a folder on your server, say:

/usr/local/apache2/www/example.php

Then a reference in example.php could be made as an absolute path, so relative to the root of the server: /usr/local/apache2/hidden/credentials.php

Or the same file could be reached using a path relative to the current file: ../hidden/credentials.php. Note that each set of ../ will move you one directory higher in your server folder structure.

Upvotes: 14

James Donnelly
James Donnelly

Reputation: 128781

A / at the start of a path will point to the root directory. This is known as an absolute path. Anything else is relative to the current folder. This is known as a relative path.

IBM describes the differences here:

An absolute path name represents the complete name of a directory or file from the /(root) directory downward. Regardless of where you are working in the file system, you can always find a directory or file by specifying its absolute path name. Absolute path names start with a slash (/), the symbol representing the root directory. The path name /A/D/9 is the absolute path name for 9. The first slash (/) represents the /(root) directory, which is the starting place for the search. The remainder of the path name directs the search to A, then to D, and finally to 9.

Unlike full path names, relative path names specify a directory or file based on the current working directory. For relative path names, you can use the notation dot dot (..) to move upward in the file system hierarchy. The dot dot (..) represents the parent directory. Because relative path names specify a path starting in the current directory, they do not begin with a slash (/). Relative path names are used to specify the name of a file in the current directory or the path name of a file or directory above or below the level of the current directory in the file system. If D is the current directory, the relative path name for accessing 10 is F/10. However, the absolute path name is always /A/D/F/10. Also, the relative path name for accessing 3 is ../../B/3.

Imagine you're using this structure, for instance, and were working in MyScript.php:

Home
  -- Foo
     -- Example.php
  -- Bar
     -- Scripts
         -- MyScript.php

If you wanted to include Example.php from within the Foo folder, you could either specify:

../../Foo/Example.php /* Relative path: ..Bar/..Home (Root)/Foo/Example.php */

Or:

/Foo/Example.php /* Absolute path: Home (Root)/Foo.Example.php */

Upvotes: 4

Related Questions