Jump to content

Meaning of realpath in PHP


Cupidvogel

Recommended Posts

Hi, can anybody explain what is meant by realpath in PHP? In particular, I was studying the documentation for the clearstatecache function in the PHP manuals, it has an optional first boolean parameter named $clear_realpath_cache, which determines whether or not to clear the realpath cache for the file mentioned as the 2nd parameter. So what difference will the absence of the first parameter make?

Link to comment
Share on other sites

realpath returns the "real" absolute path of whatever you put in.

 

If you pass it a relative path it turns it into an absolute one:

realpath('/User/Ignace/PhpStormProjects/foo/baz/../../foo'); // returns /User/Ignace/PhpStormProjects/foo

 

If the passed in argument points to a linked directory then it resolves the link and return the underlying directory, for example on my Mac the directory php5 points to the latest php5 version:

realpath('/usr/local/php5'); // returns /usr/local/php5-080920122231

 

Since all IO operations are "slow" realpath() caches everything it resolves. It uses APC for example if you have it enabled to speed up lookups across all visitors. That's what I heard/know about realpath().

 

 

Link to comment
Share on other sites

So part from the caching bit, this is just like the canonical method of the URI module in Perl which normalizes URLs, resolving unnecessary up and down traversals within the address to give it in a compact form?

 

I don't really know perl but that sounds about right.  You give it a relative path and it will determine what the actual full path is to the given file.  This includes resolving things like symlinks on linux.

 

Regarding the caching stuff, when you do something that causes PHP to lookup a stat() on a file it will cache that information so that future calls needing the same info for the same file can be resolved quickly. I find it is generally not necessary to call the clearstatcache function, but it's there if you need it.  afaik the cache is only valid for the currently executing request so if you do something with a file in request A, then do something with that same file in request B, both will go to the file for any information rather than a cache.  Clearing the cache would only be necessary if you're doing something something with the file multiple times in the same request.

 

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.