Header-Ad

PHP,Joomla,WordPress & Linux based server management, error resolutions and fixing problems.

Sunday, November 13, 2016

How to get absolute path of Current script in PHP? [Quick & Easy]

How to get absolute path of Current script in PHP?


This is the most easiest and quicker approach to this very novice matter...

Go ahead and try :

echo realpath(dirname(__FILE__));
If you place this in an included file, it prints the path to this include. To get the path of the parent script, replace __FILE__ with $_SERVER['PHP_SELF']. But be aware that PHP_SELF is a security risk!
example for URL:http://example.com/folder1/folder2/yourfile.php?var=blabla#12345

typical PHP codes:

$_SERVER["DOCUMENT_ROOT"] === /home/user/public_html
$_SERVER["SERVER_ADDR"]   === 143.34.112.23
$_SERVER['HTTP_HOST']     === example.com (or with WWW)
$_SERVER["REQUEST_URI"]   === /folder1/folder2/yourfile.php?var=blabla
__FILE__                  === /home/user/public_html/folder1/folder2/yourfile.php
basename(__FILE__)        === yourfile.php
__DIR__                   === /home/user/public_html/folder1/folder2 [same: dirname(__FILE__)]
$_SERVER["QUERY_STRING"]  === var=blabla

$_SERVER["REQUEST_URI"]   === /folder1/folder2/yourfile.php?var=blabla
parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH)  === /folder1/folder2/yourfile.php 
$_SERVER["PHP_SELF"]      === /folder1/folder2/yourfile.php

//if "YOURFILE.php" is included in "PARENTFILE.php" , and "PARENTFILE.PHP?abc"   is opened:
$_SERVER["PHP_SELF"]       === /parentfile.php
$_SERVER["REQUEST_URI"]    === /parentfile.php?abc
$_SERVER["SCRIPT_FILENAME"]=== /home/user/public_html/parentfile.php
str_replace($_SERVER["DOCUMENT_ROOT"],'', str_replace('\\','/',__FILE__ ) )  === /folder1/folder2/yourfile.php
Notice:
  • hastag (#...) url parts cant be detected from PHP (server-side). For that, use Javascript.
  • DIRECTORY_SEPARATOR returns \ for Windows-type hostings, instead of /*

For WordPress

home_url()                      //>     http://example.com
get_stylesheet_directory_uri()  //>     http://example.com/wp-content/themes/THEME_NAME  [same: get_bloginfo('template_url') ]
get_stylesheet_directory()      //>     /home/user/public_html/wp-content/themes/THEME_NAME
plugin_dir_url(__FILE__)        //>     http://example.com/wp-content/plugins/MY-PLUGIN/  [while used inside plugin.. same as:  plugins_url('',__FILE__) ]
plugin_dir_path(__FILE__)       //>     /home/user/public_html/wp-content/plugins/MY-PLUGIN/   [while used inside plugin]    



//===============MY EXAMPLES - USAGE============//
(i.e. wordpress is installed in subdirectory:  http://example.com/wpdir/)

define('domainURL',                 (((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS']!=='off') || $_SERVER['SERVER_PORT']==443) ? 'https://':'http://' ).$_SERVER['HTTP_HOST']);
    // ----->  http://example.com
define('homeURL',                   home_url());
    // ----->  http://example.com/wpdir/
define('homeFOLD',                  str_replace(domainURL,'',   homeURL));
    // ----->                    /wpdir/
define('requestURI',                $_SERVER["REQUEST_URI"]);
    // ----->                    /wpdir/any-page?with=parameters
define('requestURIfromHome',        str_replace(homeFOLD, '',requestURI) );
    // ----->                          /any-page?with=parameters
define('requestURIWithoutParametr',parse_url(requestURIfromHome, PHP_URL_PATH));
    // ----->                    /wpdir/any-page
define('currentURL',                domainURL.requestURI);
    // -----> http://example.com/wpdir/any-page?with=parameters
define('THEME_URL',                 str_replace(domainURL, '', get_template_directory_uri()) );plugin_dir_url(__FILE__)) ); 
    // -----> http://example.com/wpdir/wp-content/themes/THE-THEME-NAME/
define('PLUGIN_URL',                str_replace(domainURL, '', plugin_dir_url(__FILE__)) ); 
    // -----> http://example.com/wpdir/wp-content/plugins/THE-PLUGIN-NAME/

No comments:

Post a Comment

** Comments are reviewed, but not delayed posted **