How to find out if last part of PHP_URL_PATH is a file or directory?

5.8k Views Asked by At

Context: I'm trying to hierarchically store URI's (starting with 3758 TLD's, further refined in domains, further refined in paths, further refined in querystrings) {and divided in protocols, ports, etc), basically storing the web as one big tree and I'm trying to store it and attach meta data such as favicons, alexa rankings, SU rankings, delicious MD5 and links, etc... to the correct node by not duplicating information over nodes therefore a parent/child hierarchy is needed where realizing that the tree concept is not true but in a practical sense very useable.

Question: If I use parse_url for the path:

$url_path = parse_url($uri, PHP_URL_PATH);

What is the best way to determine if the last element is a file or a folder before splitting and storing the bits in the database? E.g. if I make a http request out to that URI part could I reliably find out the true nature of this last part of this string?

Example:

/foo/bar/imnot.php

How to reliably find out if imnot.php is a file or directory? (other than calling the developer ofcourse)

1

There are 1 best solutions below

2
On BEST ANSWER

There is no notion of "file" or "directories" when talking about URLs.

For example /foo/bar could be a valid URL that gives you information about bar but /foo/bar/node could also be a valid URL that gives you node information about bar. In this case, bar acts like both a file and a directory. This is pretty common, just take a peek at SO's URLs.