Jump to content

realpath() and dirname(__FILE__)...difference??


JsusSalv

Recommended Posts

Hello Everyone:

 

Can someone tell me what the difference is between the following:

$mypath3 = dirname(__FILE__);

$mypath4 = realpath( dirname(__FILE__));

 

I've read the php.net info and ran some tests and both produce the same results.

 

Are there different security issues related with either one?  How do you use it?

Thanks!

Link to comment
Share on other sites

They're basically the same thing, except the one with realpath() will go a little slower because it's just another function call. realpath() is nice for when you have a highly messy path such as "/home/something/somewhere/htdocs/file/../app/controller/../../core/something" and you wish to read the location where the path truly leads. If you run realpath() on a clean directory tree like "/home/something/somewhere/app/controller/" you're really just wasting cpu cycles.

Link to comment
Share on other sites

it does not give the same thing. What this latest line you gave does is take the directory path of the current file, append ".." (so, go up one dir) and then fix the path so there's no ".." in it (with realpath)

 

<?php
// Assuming file is in /path/to/file/directory/file.php
$mypath = realpath( dirname(__FILE__).'/..'); // gives: /path/to/file/
$mypath = dirname(__FILE__).'/..'; // gives: /path/to/file/directory/..
?>

 

They both point to the same directory, one is just easier to read/more optimized. In this case it is likely more efficient to use realpath() because you actually DO have a ".." to remove from the path, and if you call this many many times in the future, it's likely better to take the speed hit of realpath() in the beginning than to take the speed hit of ".." many many times in the code.

 

I'd suggest reading the php.net documentation for __FILE__, dirname(), and realpath().

Link to comment
Share on other sites

Thanks for the info.

 

"I'd suggest reading the php.net documentation for __FILE__, dirname(), and realpath()."

 

 

That's what I've been reading along with info from W3C and Tizag.  This is the reason I am here trying to get info from the community to help clarify the info.  Thanks!

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.