-1

I need current folder name via PHP. I already know basename will help this.

<?php echo basename(__DIR__); ?>

But It wont help me because of my folder structure.

Here is my weird setup;

|template.php
|/pages/
|/pages/requiredfoldername/index.php

So in my index.php it's just one line

<?php include './../../template.php'; ?>

So my question is what should i write to template.php for getting current foldername?

1
  • Maybe you're actually looking for getcwd()? php.net/getcwd Commented Dec 8, 2014 at 23:17

4 Answers 4

1
<?php
    $parts = explode("/", $_SERVER['REQUEST_URI']);

    echo $parts[3];


?>

Thank you all, I asked to my friend and he come up with this solution. And it works!!!

Sign up to request clarification or add additional context in comments.

3 Comments

An advice: If you will use mod_rewrite, 'REQUEST_URI' won't help you, but 'SCRIPT_FILENAME' will still work
@mrpn I would like to encourage you to consider the other posted answers on this page as candidates for the green tick. Using explode() is more of a workaround than a professional-grade solution. When seeking directory/path related data, there are specific functions at your disposal. Remember, too, that this page will be visited by future researchers for a long time in the future and they will want to learn the "best" way to perform this task. Please play your part in making StackOverflow a wonderful source of knowledge.
Also, in your most recent question, it seems you may have upvoted every answer posted on the page. This is not advisable when the other answers did nothing to solve your actual problem (unfortunately, they misunderstood your question/intent). Now the page is bloated with misinformation. Users concerned with gaining rep points are less willing to delete a bad/incorrect answer if it has earned them points. If you have upvoted the other answers, this is actually a bad thing for StackOverflow. Again, keep in mind the Big Picture -- StackOverflow is a resource to be trusted by researchers.
0
getcwd();

or

dirname(__FILE__);

or (PHP5)

basename(__DIR__) 

http://php.net/manual/en/function.getcwd.php

http://php.net/manual/en/function.dirname.php

Comments

0

use dirname() two times: <?php include(dirname(dirname(__FILE__)); ?>

Comments

0

This is what you need:

dirname($path)

dirname: Given a string containing the path of a file or directory, this function will return the parent directory's path.

__FILE__: The full path and filename of the file with symlinks resolved. If used inside an include, the name of the included file is returned.

--

Inside template.php, __FILE__ is the absolute path to template.php:

/[path to your app]/template.php

Then dirname(__FILE__) return:

/[path to your app]/

--

Inside index.php, __FILE__ is the absolute path to index.php:

/[path to your app]/pages/requiredfoldername/index.php

Then dirname(__FILE__) return:

/[path to your app]/pages/requiredfoldername

Also in template.php, maybe $_SERVER would help you with the key 'SCRIPT_FILENAME'. This key contains the absolute path (with exceptions in CLI) to the currently executing script:

If /pages/requiredfolder/index.php is your entry point to the app, when you include template.php, the currently executing script remains index.php. So $_SERVER['SCRIPT_FILENAME'] will be

'/[path to your app]/pages/requiredfoldername/index.php'

Using dirname with $_SERVER['SCRIPT_FILENAME'] results in:

'/[path to your app]/pages/requiredfoldername'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.