Speedy
Speedy

Reputation: 5

In PHP, how can I get the current filepath in seperate values?

Right now I'm using

$file = $_SERVER["PHP_SELF"];

to get the current filepath.. but what I really want is to get each directory in a seperate value, rather than the entire path in one value.

To be precise... I have a filesystem that basically works like

website.com/x/y/file.php
website.com/x/z/file.php
website.com/x/file.php

And I was wondering if there was a method that allowed me to get all of those directories in seperate values.

For example, if I do

echo $dirone . "/n" . $dirtwo . "/n" . $file;

That it'll end up showing as

x
y
file.php

in the case of

website.com/x/y/file.php

or, alternatively, end up showing as

x

file.php

in the case of

website.com/x/file.php

I'm honestly not sure how to pull this off. I've found methods to at least extract ONLY the filename, but that leaves me without the rest of the directories.

Upvotes: 0

Views: 43

Answers (1)

xrash
xrash

Reputation: 961

You can use:

$files = explode(DIRECTORY_SEPARATOR, $path);
foreach ($files as $file) {
    printf("%s\n", $file);
}

The DIRECTORY_SEPARATOR constant is important when dealing with paths given by the operating system, since Windows will give you '\' and unixes '/'.

Upvotes: 2

Related Questions