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
Share on other sites

__FILE__ - The full path and filename of the file. If used inside an include, the name of the included file is returned.

 

dirname - Given a string containing a path to a file, this function will return the name of the directory.

Link to comment
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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.