Solved

PHP - Locate current directory from url

Posted on 2007-04-09
6
859 Views
Last Modified: 2007-04-09
I need to be able locate the last directory of a domain, for example:

http://www.mydomain.com/mypage.php or http://mydomain.com

$currentdir = ''

or

http://www.mydomain/dir1/dir2/index.php

$currentdir ='dir2'

Example code would be much appreciated.

Thanks in advance.
0
Comment
Question by:Luv2Muff
  • 4
6 Comments
 
LVL 20

Accepted Solution

by:
steelseth12 earned 500 total points
ID: 18877469
$path =  dirname($_SERVER['PHP_SELF']);


$getslash = strrpos($path,"/");

$directory = substr($path,$getslash+1);

print $directory;
0
 
LVL 15

Expert Comment

by:Tomeeboy
ID: 18877477
This should work for you.  Be sure to change the $base_dir to whatever your base path is for your web directory.

<?php

$base_dir = "/your/base/web/directory";
$script_dir = getcwd();

if ($script_dir == $base_dir) {
     $current_dir = "";
} else {
     $current_dir = array_pop(explode($script_dir, "/"));
}

?>

0
 
LVL 15

Expert Comment

by:Tomeeboy
ID: 18877579
If your file is in the base directory of your web site, I don't think steelseth12's method would work.  In that scenario, I think $_SERVER['PHP_SELF'] would return just the name of the file, which would then result in the $directory variable being set to that file name (minus the first character).
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Expert Comment

by:Tomeeboy
ID: 18877676
Actually, I think I overlooked something in steelseth12's solution.  Because of the use of dirname(), you may end up with just a slash or a dot as the path.  If that were to happen, then substr is going to return FALSE to the $directory variable.  Technically, I think it would still work, though I'm not sure if it's really the best way to do it.  I don't know if that would be the most reliable, or safe, method of getting the current directory (especially since I don't know what you're using this for).
0
 

Author Comment

by:Luv2Muff
ID: 18877731
I accepted the answer from steelseth12

It was the first answer in, I have tested to 1 directory deep, and it seems to work perfectly.

Many, many thanks for all your help.

Luv2.
0
 
LVL 15

Expert Comment

by:Tomeeboy
ID: 18877870
Ouch, 65 minutes vs. 66 minutes :)  Oh well, glad it works for you.  If you run into any issues, the answer I submitted should work without any problems.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now