1

I am having a slight trouble. I am wanting to get a find the top level folder and use it as a variable.

For instance.

If I have a url: http://www.someurl.com/foldername/hello.php

I would like to look at the URL and echo 'foldername'.

At present my code strips things so that it echos 'hello.php'.

Any help would be greatly appreciated.

<?php

// Get URL String // 
function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}

// Split String and get folder//

function curPageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/"));
}

// Pass along Results //
$foldername = curPageName();

echo $foldername;

?>

3 Answers 3

1
          <?php 

            $a = "http://www.someurl.com/foldername/hello.php";

            $b =  explode('/',$a);

            //var_dump($b); 

        // i got this result  array(5) { [0]=> string(5) "http:" [1]=> string(0) "" [2]=>                  string(15) "www.someurl.com" [3]=> string(10) "foldername" [4]=> string(9) "hello.php" }

// now to get or echo foldername store the value from the array in a variable

         $value = $b['3'];

        echo $value // will output foldername 
      ?>

i am a newbie too

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

4 Comments

That's a great result and what I was looking for. The only trouble I have is to dynamically feed the URL into the $a. e.g. $a = "$url"; so I am sure from here I can put something together.
ya whatever url value you are getting you can store it in $a and get you answer . dont forget to accept or upvote the answer if it has helped you :)
you can get url look at this method here phpeasystep.com/phptu/27.html
Cheers Rinzler! Very stoked with your help!
1

Try it with

$parse = parse_url('http://www.someurl.com/foldername/hello.php')
echo dirname($parse['path']);
// Output: /foldername
// use trim() to strip of that leading slash

Comments

1

Should we do so tough?

i think this would do

function getFolderName($URL)
{
        $pieces  = explode("/",$URL);
    //    return $pieces[3];// -->do this if you  need folder after domain
    return $pieces[count($pieces)-2]; //-->do this if you need folder just before sourcefile
}

3 Comments

This will return the second last instead of the first part of the path after the hostname. Consider the url http://somedomain.com/foldername/otherfolder/hello.php where the function proposed by you will deliver otherfolder instead of foldername
no, it will return the folder name just before the source file.
It will return path before the source file and i think that is what the original poster wanted.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.