Directory Lister is a simple PHP based script created to let you list the contents of a directory and all it's sub-directories and allow you to navigate there within. This in-depth comparison of phpmydirectory.com and phplinkdirectory.com might explain which of these two domains is more popular and has better web stats.
Active1 year, 6 months ago
In PHP, what would be the cleanest way to get the parent directory of the current running script relative to the www root? Assume I have:
Or just:
And I need to get
/relative/path/to/ with slashes properly inserted. The hare psychopathy checklist revised pdf printer. Lexmark z32 printer driver windows 7. What would you suggest? A one liner is preferred.
EDIT
I need to get a path relative to the www root,
dirname(__FILE__) gives me an absolute path in the filesystem so that won't work. $_SERVER['SCRIPT_NAME'] on the other hand 'starts' at the www root.
Tatu Ulmanen
Tatu UlmanenTatu Ulmanen
Phpmydirectory Script Fonts
101k2828 gold badges165165 silver badges178178 bronze badges
![]() 13 Answers
If your script is located in
/var/www/dir/index.php then the following would return:
or
Edit
This is a technique used in many frameworks to determine relative paths from the app_root.
File structure:
index.php is my dispatcher/boostrap file that all requests are routed to:
library.php is some file located an extra directory down and I need to determine the path relative to the app root (/var/www/). Dell laser printer 1710 driver.
There's probably a better way to calculate the relative path then Mike BMike B
str_replace() but you get the idea.
27.1k1010 gold badges7575 silver badges103103 bronze badges
As of PHP 5.3.0 you can use
__DIR__ for this purpose.
The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__ FILE__).
See PHP Magic constants.
Charlie VieillardCharlie Vieillard
If I properly understood your question, supposing your running script is
This would give you the parent directory of your running script relative to the document www:
If you want the parent directory of your running script relative to server root:
Marco DemaioMarco Demaio
20.7k3131 gold badges112112 silver badges148148 bronze badges
hanshans
Sliq
9,4522121 gold badges8585 silver badges133133 bronze badges
Phpmydirectory Scriptures
BradBrad
Jordan Ryan MooreJordan Ryan Moore
6,43722 gold badges2222 silver badges2727 bronze badges
Here is what I use since I am not running > 5.2
Double dirname with file as suggested by @mike b for the parent directory, and current directory is found by just using that syntax once.
Note this function only returns the NAME, slashes have to be added afterwards.
AlexAlex
7,23955 gold badges2626 silver badges6868 bronze badges
Vignesh KMVignesh KM
Try this. Works on both windows or linux server.
str_replace(','/',dirname(dirname(__FILE__)))
cestarcestar
collidoscopecollidoscope
This is a function that I use. Created it once so I always have this functionality:
JohnJohn
Goodhope Kudakwashe DhliwayoGoodhope Kudakwashe Dhliwayo
Got it myself, it's a bit kludgy but it works:
So if I have Tatu UlmanenTatu Ulmanen
/path/to/folder/index.php , this results in /path/to/ .
101k2828 gold badges165165 silver badges178178 bronze badges
Not the answer you're looking for? Browse other questions tagged phpdirectory or ask your own question.Comments are closed.
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |