• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • About
  • Projects
    • GStaticMap WP Plugin
  • Contact
  • Privacy Policy

Lorenz Blog

All About Web & Mobile Application Development

  • Featured Articles
  • Gadgets
    • Android
    • Blackberry
  • Programming
    • Android
    • PHP
    • Java Script
    • MySQL
    • Postgresql
    • Flex
    • Web
  • Software
    • Mac OS
    • Windows
    • Linux
  • Web
You are Here » Home >> Information Technology >> Programming >> PHP >> How To Define Global Absolute Path in PHP

How To Define Global Absolute Path in PHP

October 31, 2009 by Lorensius Londa 8 Comments

A path is general form of a filename or directory that specified a unique location in a file system. In web programming, a path  is used to define links (url) to documents and images or to include a specified library file. There are two standard ways to define a path:

  1. Absolute Path, is a path that points to the same location on file system regardless of the working directory or combined paths and usually written in reference to root directory.
  2. Relative Path, is a path  that relative to working directory.

In many ways, absolute path is much usefull because it doesn’t need us to redefine the path or link if we change the working directory. For an example, below is a directory hierarchy of a web application that located on a development server  that can be accessd via url www.mysite.com, where /var/www/html is it’s web server’s document root and application root directory and ‘/’ is the application root url.

Document Root
Document Root

Take a look at index.php in profile directory, to define an url to mypic.jpg in images directory and include lib.php from lib directory:

using absolute path:

<?php
include '/var/www/html/lib/lib.php';

$str = '<img src="/images/mypic.jpg">';
?>

using relative path:

<?php
include '../../lib/lib.php';

$str = '<img src="../../images/mypic.jpg">';
?>

Using absolute path, it wouldn’t be a matter where the working directory is, but it would be a problem if  you change the application root directory into  another directory other than web server’s document root, for example in /var/www/html/web, so your application would be accesed via www.mysite.com/web. We have to edit manually each file that using absolute path because the root directory will changed to /var/www/html/web so the included file will not work.

To overcome this problem, you have to define a global variable that specified absolute path that generally applied to all directories in your application. The  solution is to define two variables which will be used to define root directory for file operations and root url for url operations  in a file that would be placed in the root directory .

<?php
define('ROOT_DIR', dirname(__FILE__));
define('ROOT_URL', substr($_SERVER['PHP_SELF'], 0, - (strlen($_SERVER['SCRIPT_FILENAME']) - strlen(ROOT_DIR))));
?>

Save it as init.php and place it in your application root directory.So, from our example above, using the two variables:

<?php
include '../../init.php';
include ROOT_DIR .  '/lib/lib.php';

$img = '<img src="' .  ROOT_URL . '/images/my.pic.jpg">';
?>

As you can see, it will be applied in any directories regardless where the current working directory or server document root.

Facebooktwitterredditpinterestlinkedinmailby feather

Related posts:

  1. How to Change Application Theme on Adobe Flex Builder 3
  2. How to List A Directory Contents Using PHP (Recursive)
  3. How to Create Facebook Application with PHP for Beginner
  4. How to Crop Image Using JavaScript and PHP

Filed Under: Featured Articles, Information Technology, PHP, Programming Tagged With: Absolute Path, PHP, Programming, Relative path

About Lorensius Londa

Passionate web and mobile application developer. Co-founder of TRUSTUDIO, loves programming, Android, aviation, travelling, photography, coffee and gym mania.

Reader Interactions

Comments

  1. Rodrigo says

    January 20, 2010 at 3:57 am

    Hi,

    What do you do when you have a sub-web, /var/www/html/web/sub-web and you want to upload files in a subdirectory images
    /var/www/html/web/images, notice that images is also subdirectory like sub-web. How do you define the path?

    Reply
    • lorenz says

      January 26, 2010 at 12:19 am

      hi, sorry for the late reply,

      define a constant ROOT_DIR : define(‘ROOT_DIR’, dirname(__FILE__)); and save it on a file (ex: root.php) in /var/www/html/web. If you want to upload files to /var/www/html/web/images from your sub-web directory, just include the root.php and ur upload path would be ROOT_DIR . ‘/images’.

      Reply
  2. imp-ressions says

    January 9, 2012 at 11:21 pm

    Thanks Lorenz. Just what I needed! Thanks for posting.

    Reply
  3. ilia says

    March 24, 2013 at 8:25 am

    Not Found

    The requested URL /home/misterge/domains/mister.ge/public_html/index.php was not found on this server.

    Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

    Can i fix this thing with this?
    http://games.mister.ge/

    Reply
  4. Vijesh says

    January 3, 2017 at 1:19 am

    Thanks mate, this is what I am finding since last two hours, can you also tell me is it after implementing this, is it necessary to use set_include_path and get_include_path function, I am not sure about this, please help.

    Reply
  5. Shedrack says

    March 17, 2017 at 2:23 pm

    I try writing a PHP code to save data to my MySQL database and to save my image to the folder created in my htdocs but nothing is happening.

    Image folder directory (“image/$image”) where image is the name of the folder and $image is the variable name holding my values. Please I want you to help me out.

    Reply

Leave a Reply Cancel reply

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

Primary Sidebar

About Me

A husband, father of two, passionate software developer, diy lover and home baker who loves to learn new things. Read More…

  • Facebook
  • GitHub
  • Google+
  • Instagram
  • Twitter
  • YouTube

Featured Articles

How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Blue Bamboo P25 Printer Android Demo Application With Source Code

Simple JSON RPC Client for Android

How to Send Message to Google Cloud Messaging (GCM) Server Using JSON and PHP

Footer

Recent Comments

  • Aditya Dabas on About
  • Ayten Göksenin Barutçu on How to Make Android Map Scrollable Inside a ScrollView Layout
  • mang jojot on About
  • Hussain on How to Programmatically Scan or Discover Android Bluetooth Devices

Recent Posts

  • How to Fix Blank Screen on WordPress Add/Edit Post Page
  • How to Programmatically Restart the ESP32 Board
  • How to Get Hardware Info of ESP32
  • How to Setup MQTT Server Using Mosquitto and Libwebsocket on Freebsd

Latest Tweets

  • @tricahyono_bowo @infobandung @infobdg Wah kejauhan om 355 days ago
  • Wilujeng enjing sadayana..Mohon info tempat powder coating dan sandblasting yg recommended di Bandung dunk @infobandung @infobdg359 days ago

Copyright © 2023 · Magazine Pro on Genesis Framework · WordPress · Log in