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! Quote Link to comment 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. Quote Link to comment 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? Quote Link to comment 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. Quote Link to comment 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__) Quote Link to comment 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. Quote Link to comment 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! Quote Link to comment 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(). Quote Link to comment 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.