Solved

PHP - Locate current directory from url

Posted on 2007-04-09
6
862 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

776 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