Code of the Day: PHP, get the current directory of a page.


Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380

Warning: WP_Syntax::substituteToken(): Argument #1 ($match) must be passed by reference, value given in /home/bateeqjg/public_html/news/wp-content/plugins/wp-syntax/wp-syntax.php on line 380
1
2
3
4
5
6
7
8
// Returns the current page.
function getPageURL(){
	return 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
}
// getCurrDir() returns the current directory.
function getCurrDir(){
	return dirname( getPageURL() );
}

Example:

1
2
getPageURL();    // returns "http://localhost/live/uploadProductImage.php"
getCurrDir();    // returns "http://localhost/live/"
(Page view Count: 42)

Published by

Larry Battle

I love to program, and discover new tech. Check out my stackoverflow and github accounts.