Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

what's the difference between

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

and

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

How will include path will differ?

like image 457
user1463822 Avatar asked Oct 15 '13 12:10

user1463822


People also ask

Should I use require or include PHP?

Use require when the file is required by the application. Use include when the file is not required and application should continue when file is not found.

What is difference between include_once () and include ()?

The include() function is used to include a PHP file into another irrespective of whether the file is included before or not. The include_once() will first check whether a file is already included or not and if it is already included then it will not include it again.

What is the function include () used for?

The Include() function is used to put data of one PHP file into another PHP file. If errors occur then the include() function produces a warning but does not stop the execution of the script i.e. the script will continue to execute.


2 Answers

/.. 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.

like image 131
Jelle Ferwerda Avatar answered Oct 16 '22 17:10

Jelle Ferwerda


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 */
like image 29
James Donnelly Avatar answered Oct 16 '22 16:10

James Donnelly