Jump to content

dirname(__FILE__) how this works


sowmithrii

Recommended Posts

 

i dont understand the purpose of dirname(__FILE__) function.

 

i created my sites folder MYSITE, and in that few subfolders FOLDER1 , FOLDER2, FOLDER3. I added few files to MYSITE folder like index.php, register.php, login.php... and so. and one file to all sub folders like... folder1.php, folder2.php, folder3.php as their names reflect.

 

when i visit my site... http://MYSITE.com/MYSITE    and if i have used dirname(__FILE__) in index.php, folder1.php, folder2.php, folder3.php

 

And i have nagivation from index.php to folder1.php from there to folder2.php, from here to folder3.php

 

what will be the result of dirname(__FILE__), when i am in index.php and in folder1.php, folder2.php, folder3.php  , when each page prints that dirname(__FILE__) functions output.

 

am new to php... please explain me clearly.. its a request.

 

thanks and regards..

sowmithrii

Link to comment
https://forums.phpfreaks.com/topic/79948-dirname__file__-how-this-works/
Share on other sites

The dirname(__FILE__) trick is invaluable. Let's say you have a text file in the same folder as the include and you want the included file to load it.

As you know PHP runs the included files as a pasted chunk of code but works on its current directory not the includes.

This is where dirname(__FILE__) returns the actual folder of the include and you can load a file located in same folder for internal use.

 

Hope it's clear :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.