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
https://forums.phpfreaks.com/topic/133294-realpath-and-dirname__file__difference/
Share on other sites

There's no huge difference. Some server configurations will include "." in the __FILE__ path. realpath() will remove this "." and give the full file path. Other than those rare rare situations, where it won't even make a difference anyway, I can't foresee them giving different values.

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.

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().

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!

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.