Suppose you are at a URL http://yoursite.domain/dir1/subdir2/subdir/file.php and want to get the 1st level Parent directory of this URL. Evidently, the URL of the parent Directory will be http://yoursite.domain/dir1/subdir2/subdir but the question is how to generate it automatically using PHP.
The answer is using PHP function dirname(). Using this, you can easily get any level of parent directories of the current URL. Before you check the code, how about having a look at the online demo first?
To know how to use dirname to get the parent directory of the required level of current URL, read on below!
The basic Usage of dirname() function:
The dirname function gives one level up directory name (absolute or relative according to the source provided) of the passed argument. The syntax is:
dirname($string)
For example:
echo dirname('/mysite/mydir/mydir2/');
The output of the above code will be: /mysite/mydir
It can also be applied over Web URL. So the output of the following code will be:
dirname('http://www.intechgrity.com/search/label/MySQL')
Output: http://www.intechgrity.com/search/label
Using dirname to get The parent directory of current working URL:
Now, we will see how to get the parent directory of the working URL. Say, my working URL is http://www.intechgrity.com/2010/05/create-login-admin-logout-page-in-php-w.html
Clearly, the parent directory of this URL is
http://www.intechgrity.com/2010/05
Now all we need to do, is to pass this string through dirname function… Following is an example:
$url = 'http://www.intechgrity.com/2010/05/create-login-admin-logout-page-in-php-w.html'; echo dirname($url);
The above code will output the result as shown before.
Getting certain level of parent directory using dirname:
Say, you want to get 2 level up directory name of the Working URL. So, you want http://www.intechgrity.com/2010/05/create-login-admin-logout-page-in-php-w.html to become http://www.intechgrity.com/2010
The answer is simple… We have to pass the URL through dirname function, for required number of levels… Obviously we can use a for loop to do this automatically.
$url = 'http://www.intechgrity.com/2010/05/create-login-admin-logout-page-in-php-w.html'; $number = 2; echo "<p>The $number level Parent Directory name of $url is</p>"; for($i=0; $i<$number; $i++) { $url = dirname($url); } echo '<blockquote>'.$url.'</blockquote>';
This will pass the URL http://www.intechgrity.com/2010/05/create-login-admin-logout-page-in-php-w.html 2 times through dirname function and will output the result.
Note that the above statement is equivalent to the following code [Provided that $number is 2]
$url = 'http://www.intechgrity.com/2010/05/create-login-admin-logout-page-in-php-w.html'; //Pass it through two successive dirname $url = dirname(dirname($url)); //Now echo the output echo $url;
That was all. Check the Online Demonstration to try out yourself. Put any URL and enter the number of level you want to go up. Also download the source code to see how things are done!
I hope the information was useful for you. Don’t forget to give your feedback! Also if you face any problem, feel free to ask here!
Pingback: Determine Plugin path n URL using Wordpress API functions | InTechgrity
my url is : http://localhost/tourism/hotel.php
i need to pass this url in href,and i wanna show this url : http://localhost/tourism/
how is it possible????help plssss…waiting sir……
i want http://www.example.com/new-registration.php is convert to http://www.example.com/new-registration , how to convert this url inform me?
Thanks in advance..,
You will need to have htaccess rewrite for this. I shall write a new post on this sometime. You can simply google about htaccess rewrite.