What's the best way to get the Directory of a URL using php?

Active3 hr before
Viewed126 times

8 Answers


Im trying to set active classes in a nav using php. Now I need to set it by directory rather than complete URL as I have a main landing page for each directory with sub navigation for the other pages within the directory. I was going to use the following but this returns the full url., Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,Your suggestions on how to modify this to return the directory instead would be much appreciated.,You could get the full URL and then explode it to get down to your directory, like so:

If you want to extract the directory name from an URL you can use parse_url and dirname as:

$url = 'http://www.strangeleaves.com/nexus/index.php';
$arr = parse_url($url);
$path = dirname($arr['path']); // $path is now /nexus
load more v

dirname — Returns a parent directory's path, dirname() operates naively on the input string, and is not aware of the actual filesystem, or path components such as "..". ,realpath() - Returns canonicalized absolute pathname, The number of parent directories to go up.

/etc /
(or\ on Windows)
C: \

Create a PHP variable which will store the URL in string format.,Append the regular symbol, i.e. “://” to the URL.,Check whether the HTTPS is enabled by the server .If it is, append “https” to the URL string. If HTTPS is not enabled, append “http” to the URL string.,Note: Use isset() function to check whether HTTPS is enable or not. The isset() function is used to check a variable exists or not.

load more v

http://www.domain.com/folder1/folder2/THIS_ONE/file.php,in a nutshell: take URI, and do preg_split() for ‘/’…,You’re not seriously using preg_split when you have such a simple expression, do you? Use explode() for splitting a string at a character; it is much faster

in a nutshell: take URI, and do preg_split() for ‘/’…

$path = $_SERVER['REQUEST_URI']; // this gives you /folder1/folder2/THIS_ONE/file.php
$folders = preg_split('/', $path); // splits folders in array
$what_we_need = $folders[3];
load more v

To get the current page URL, PHP provides a superglobal variable $_SERVER. The $_SERVER is a built-in variable of PHP, which is used to get the current page URL. It is a superglobal variable, means it is always available in all scope.,To get only name of the current page opened at browser, see the below example:,Or, we can also get the full URL of current page using another way given in the next example. ,If we want the full URL of the page, then we'll need to check the protocol (or scheme name), whether it is https or http. See the example below:


Search Answer Titles

< ? php
echo $uri; // Outputs: URI 

$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$url = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
echo $url; // Outputs: Full URL 

$query = $_SERVER['QUERY_STRING'];
echo $query; // Outputs: Query String
load more v

To create the symbolic link, you may use the storage:link Artisan command:,The exists method may be used to determine if a file exists on the disk:,You can set the visibility when writing the file via the put method:,The missing method may be used to determine if a file is missing from the disk:

When using the local driver, all file operations are relative to the root directory defined in your filesystems configuration file. By default, this value is set to the storage/app directory. Therefore, the following method would write to storage/app/example.txt:

use Illuminate\ Support\ Facades\ Storage;

Storage::disk('local') - > put('example.txt', 'Contents');
load more v

../../file.php (file is in the folder that is two levels higher than the current directory),../file.php (file is in the folder that is one level higher than the current directory),images/picture.jpg (the file is in the images folder that is in the current directory),./file.php (the file is in the current folder. The same as just file.php)

Some absolute path examples:

/var/www / site / forum / index.php / img / frame.gifC: \windows\ command.com

So now you can tell that to make any file system path work, it should be absolute and built using DOCUMENT_ROOT. So a correct PHP code to access /forum/index.php from PHP would be


Imagine your site has two sections,

http: //www.example.com/about/info.phpandhttp://www.example.com/job/vacancy.php

For example, if your application is hosted in /var/www/app and there are two subfolders, /var/www/app/bin and /var/www/app/config, and you want to access the latter from the former, you can write the following code:

$config_path = __DIR__.
require $config_path;

Then we could write the following code (among other things) in bootstrap.php:

define('ROOT_DIR', realpath(__DIR__.

And then in both index.php and console.php the

require __DIR__.

to make all the bootstrap stuff available, including the ROOT_DIR constant. From now on we can use it to build absolute paths starting from the root directory (as long as our scripts are called through the entry point either web or console one):

include ROOT_DIR.
load more v

Other "using-directory" queries related to "What's the best way to get the Directory of a URL using php?"