Solved

PHP - Locate current directory from url

Posted on 2007-04-09
6
863 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 Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

820 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