0

I wanted to ask whats the difference between

include('./config.php')

and

include('../config.php')

The 2nd one goes one dir up and includes from folder one level higher. But what Exacly does one dot do?

Edit; Thank you everyone for you answers. It brightened my mind a bit. I chose answer that was most infomative about topic (especially in PHP enviroment)

Grzegorz
  • 3,538
  • 4
  • 29
  • 47

6 Answers6

11

In posix file systems . simply means current directory and .. means parent directory.

datasage
  • 19,153
  • 2
  • 48
  • 54
3

One dot refers to the current directory.

include('./config.php')

is basically the same as

include('config.php')
Kara
  • 6,115
  • 16
  • 50
  • 57
3

This is a system thing and NOT just a PHP thing.

The ./ indicates the current directory. If you ever list the contents of a *nix system you will get the following at the top.

.
../

The top one (.) is the same as ./ which means "this directory". So if including a file like such:

include('./config.php')

You are telling PHP to look in the current directory for "config.php". Which is the same as

include('config.php')

The ../ indicates the directory above or "parent directory"

include('../config.php')

This is telling PHP to go one directory up and look for "config.php". These commands can be chained like so:

../../config.php

This tells the system to go up one directory, go up again and then look for "config.php"

UnholyRanger
  • 1,977
  • 14
  • 24
  • Hmm, wondering now. In PHP when you include file, it includes file that is relative to the file that is Executed, not included. Basicly, when I have file test/index.php and include test/library/config.php and in this file I include file with or without ./ it will look for files in test/ directory in both cases? – Grzegorz Mar 21 '13 at 17:03
  • @Gacek, yes. When including a file, it's as if the contents of the script is in the current file. If you need relative to the included file, use `dirname(__FILE__)` – UnholyRanger Mar 21 '13 at 17:05
  • I know about dirname(__FILE__). just was wondering aboug difference between ./ and without. If there is any difference except some people write it and some do not. Mostyl I always try to include absolute path since in case of executing script from CLI it will not fond files unless in right directory. – Grzegorz Mar 21 '13 at 17:08
  • @Gacek oh.. read your second question wrong. the `./` is better (IMO) but is not needed. With or without is still using relative path – UnholyRanger Mar 21 '13 at 17:11
  • I know ./ is used to ./configure and executing scripts in unixes, since configure only would not work. so I was wondering if it has anything to do in this case :) – Grzegorz Mar 21 '13 at 17:13
  • @Gacek The `./` is needed when going to execute a script. In this case, you're only including – UnholyRanger Mar 21 '13 at 17:23
1

. means the current directory

.. means the parent directory

It is not about PHP, it is OS convention that is called dot directory name

"dirA\.\dirB" is equivalent to "dirA\dirB".

"dirA\dirB\..\dirC" is equivalent to "dirA\dirC".

Community
  • 1
  • 1
iTech
  • 18,192
  • 4
  • 57
  • 80
0
include('../config.php') :- parent directory
include('./config.php') :- current directory
Sumit Bijvani
  • 8,154
  • 17
  • 50
  • 82
0

./ is for the current directory

../ is for going to the parent directory of the current one

For answering your question, suppose you don't write .

include('/config.php') would include the 'config.php' file in the root of the filesystem

Francisco Presencia
  • 8,732
  • 6
  • 46
  • 90