Craig van Tonder
Craig van Tonder

Reputation: 7687

PHP - Get name and directory of current script excluding domain?

I am working on a product catalog. The directory structure is 3 levels deep from the domain root.

So say for instance I have a product page for an orange. And the orange is in a sub category called citrus, which is in a category called fruit which is in a directory called products. so the real path to this page would be http://www.mydomain.com/products/fruit/citrus/orange.php

Now, how would I get only the following: /fruit/citrus/orange when running orange.php

I have tried : dirname(__FILE__); but this gives an output of http://www.mydomain.com/products/fruit/citrus/orange which includes the domain which I do not want.

Could anyone offer any suggestions or advice on how I could accomplish something like this?

Thank you!

Upvotes: 0

Views: 557

Answers (1)

DaveRandom
DaveRandom

Reputation: 88697

$levels = 3;
$temp = array();

$parts = explode('/', $_SERVER['REQUEST_URI']);
for ($i = 0; $i < $levels; $i++) {
  $temp[] = pathinfo(array_pop($parts), PATHINFO_FILENAME);
}
$temp[] = '';

$result = implode('/', array_reverse($temp));

Upvotes: 1

Related Questions