SirMaxime
SirMaxime

Reputation: 154

How to extract a specific folder's name in PHP?

Say I have this:

folder-one/folder-2/folder-3/myfile.php

How would I be able to find the name of "folder-2"? I know that if I use basename(__DIR__) I will get 'folder-3' but I really need to find folder-2 and folder-one?

Upvotes: 0

Views: 61

Answers (2)

Paul
Paul

Reputation: 141927

Use dirname with basename.

If __DIR__ is 'folder-one/folder-2/folder-3', then you can just use:

$dir = basename( dirname( __DIR__ ) );

Upvotes: 2

dirname(__FILE__)

Then explode etc etc.

php.net dirname

Upvotes: 0

Related Questions