Directory handling in PHP is pretty much the same and easier as in C. To fetch the current working directory in PHP – dirname( __FILE__ ) will do, but to fetch the current path url we need to rely on $_SERVER, which holds all the information like host name, currently executing script, protocol ( http or https ) and so on. You can find tons of manual on this $_SERVER, so I am not going to write another manual page for this PHP variable. I have written a function that will return the current path url as I did earlier with JQuery – ( get the path of executed JS file ).

fetching url

Here is the few lines of code written as a function so you can call wherever you want.

function my_url(){
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
return $url;

Now call your function like this

$url = my_url();

This will return the current path url. I have also extended this basic function into a PHP class that gives you more option in url manipulation.

class myUrl
var $protocol;
var $site;
var $thisfile;
var $real_directories;
var $num_of_real_directories;
var $virtual_directories = array();
var $num_of_virtual_directories = array();
var $baseurl;
var $thisurl;
function myUrl()
$this->protocol = ( isset( $_SERVER["HTTPS"] ) && strtolower( $_SERVER["HTTPS"] ) == "on" ) ? 'https' : 'http';
$this->site = $this->protocol . '://' . $_SERVER['HTTP_HOST'];
$this->thisfile = basename($_SERVER['SCRIPT_FILENAME']);
$this->real_directories = $this->cleanUp(explode("/", str_replace($this->thisfile, "", $_SERVER['PHP_SELF'])));
$this->num_of_real_directories = count($this->real_directories);
$this->virtual_directories = array_diff($this->cleanUp(explode("/", str_replace($this->thisfile, "", $_SERVER['REQUEST_URI']))),$this->real_directories);
$this->num_of_virtual_directories = count($this->virtual_directories);
$this->baseurl = $this->site . "/" . implode("/", $this->real_directories) ;
$this->thisurl = $this->baseurl . implode("/", $this->virtual_directories) ;
if( strcmp ( substr( $this->thisurl, -1 ), "/" ) != 0 )
$this->thisurl = $this->thisurl."/";
if( strcmp ( substr( $this->baseurl, -1 ), "/" ) != 0 )
$this->baseurl = $this->baseurl."/";
function cleanUp($array)
$cleaned_array = array();
foreach($array as $key => $value)
$qpos = strpos($value, "?");
if($qpos !== false)
if($key != "" && $value != "")
$cleaned_array[] = $value;
return $cleaned_array;

This class can return base url, current path url, executing script, hostname, protocol (http or https) etc. This is basically using the $_SERVER variable to manipulate the path and url.

You create an instance for this class and access the values like this

$dir = new myUrl( );
echo "This url: ".$dir->thisurl;
print "<br />";
echo "Site: ".$dir->site;
print "<br />";
echo "This file: ".$dir->thisfile;

You can put this whole class into a php file and include it wherever you want, it will make easier for you in url manipulation within php.

Get the current path url in php
Tagged on:                                 

3 thoughts on “Get the current path url in php

  • at

    I love this, keep it up it solve my problem easily…

  • at

    Hello friend,

    Well done , it is very beautiful and useful example as always ! ! !

    We learnt a lot from you’re nice example ! ! !

    You are a code saver

    Thank you in advance.

    Best Regards,


Leave a Reply

Your email address will not be published. Required fields are marked *