JsusSalv Posted November 19, 2008 Share Posted November 19, 2008 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 More sharing options...
genericnumber1 Posted November 19, 2008 Share Posted November 19, 2008 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. Link to comment https://forums.phpfreaks.com/topic/133294-realpath-and-dirname__file__difference/#findComment-693247 Share on other sites More sharing options...
JsusSalv Posted November 19, 2008 Author Share Posted November 19, 2008 What about security issues? Is one preferred over the other? Is this used more in OOP than in procedural coding? Link to comment https://forums.phpfreaks.com/topic/133294-realpath-and-dirname__file__difference/#findComment-693254 Share on other sites More sharing options...
genericnumber1 Posted November 19, 2008 Share Posted November 19, 2008 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 https://forums.phpfreaks.com/topic/133294-realpath-and-dirname__file__difference/#findComment-693257 Share on other sites More sharing options...
redarrow Posted November 19, 2008 Share Posted November 19, 2008 I personaly think if you write the path out your self it quicker......... but if the path has got unusall structure then it worth using dirname( __file__) Link to comment https://forums.phpfreaks.com/topic/133294-realpath-and-dirname__file__difference/#findComment-693259 Share on other sites More sharing options...
genericnumber1 Posted November 19, 2008 Share Posted November 19, 2008 dirname(__FILE__) is actually a nice call-once function at the beginning of your code to allow script portability, red. Link to comment https://forums.phpfreaks.com/topic/133294-realpath-and-dirname__file__difference/#findComment-693261 Share on other sites More sharing options...
JsusSalv Posted November 19, 2008 Author Share Posted November 19, 2008 I found an application that uses the following: $mypath = realpath( dirname(__FILE__).'/..'); Does the same thing as the two examples I provided above. Can anyone describe the logic behind this example? Thanks! Link to comment https://forums.phpfreaks.com/topic/133294-realpath-and-dirname__file__difference/#findComment-693269 Share on other sites More sharing options...
genericnumber1 Posted November 19, 2008 Share Posted November 19, 2008 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 https://forums.phpfreaks.com/topic/133294-realpath-and-dirname__file__difference/#findComment-693589 Share on other sites More sharing options...
JsusSalv Posted November 19, 2008 Author Share Posted November 19, 2008 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 https://forums.phpfreaks.com/topic/133294-realpath-and-dirname__file__difference/#findComment-693777 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.