Codeigniter: Helper – absolute path helper

Recently I decided to give some more time to my blog and write some short but interesting posts. I will be writing some posts about custom helper functions that I have written which helps me in application development.

First on the list is a absolute path helper which creates an absolute path, relative to the base of the application. I call this function base_path(‘relative/url’);

  1. if(!function_exists(‘base_path’)){
  2.     function base_path($relative_path_location){
  3.         return base_url().$relative_path_location;
  4.     }
  5. }

The above is the code which will go into your helper file. The function formulates an absolute url from the given relative path based on base path of the application directory. It uses the existing base_url() helper function in codeigniter. So, you have make sure the URL helper is already loaded.

Example:

Suppose you want to tag an image which is located at “application-root/images/picture.jpg” to give its absolute URL in the img tag you can type as follows in your template/view file.

  1. <img src="<?php echo base_path(‘images/picture.jpg’);?>" alt="picture"/>

If you application is hosted at domain.com then it will give http://www.domain.com/images/picture.jpg as the returned path where http://www.domain.com/ is the base_url();

thanks,

thephpx

Share

About thephpx

I am a freelance web application developer. I work with PHP5 and MySQL. Most of my projects are done using the codeIgniter framework.I am also working towards moving onto the Zend Framework. I follow agile development methodology and manage my projects throuh SCRUM based approach. Apart from being a developer I am also the CEO of a small web identity and application development firm located in dhaka, bangladesh. The firm known as 'implode impressions' also follows agile development techniques and works under the SCRUM based approach.

One response to “Codeigniter: Helper – absolute path helper

  1. mik

    Greats,

    use base_url() . ‘images/picture.jpg’

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>