Solved

PHP - Locate current directory from url

Posted on 2007-04-09
6
856 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 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

14 Experts available now in Live!

Get 1:1 Help Now