简体   繁体   中英

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.

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 '/'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM