0

I'm developing a website on my localhost. I have a problem with urls.

for a url like this

http://localhost:81/my_website/library

I should output image src this way :

<img src="<?php print $directory; ?>/image/header/headerR.png"/>

but when the above url is changed to this

http://localhost:81/my_website/library/

I should output image src this way :

<img src="../<?php print $directory; ?>/image/header/headerR.png"/>

I mean it should refer to the parent. my website has also contextual filter which must be inserted after library/ .is there any general solution for handling such thing?

  • 1
    Why don't you use these global variables: $base_path or $base_url for your stuff. – Stone Vo Oct 3 '13 at 5:36
  • I tested $base_path and the same problem happened – M a m a D Oct 3 '13 at 6:40
  • Do you remove the comment of this line $base_url = 'http://www.example.com' in settings.php and change the www.example.com by your domain. It should be like $base_url = 'http://localhost:81/my_website'; in your settings.php file – Stone Vo Oct 3 '13 at 7:05
3

Use the global variables for resolving the issue of path of files as below:

  1. Use base_path if your fine with domain name not to be included: It will return path respect to the drupal root.
    Eg: "/" for drupal root(http://localhost:81/my_website), and
    "/images/header" for http://localhost:81/my_website/image/header/header

    Example:

    /*Assuming image path: http://localhost:81/my_website/image/header/headerR.png*/
    global $base_path; /* will return "/" for http://localhost:81/my_website*/
    $image_path = $base_path . 'image/header/headerR.png';
    

    Note: Don't use trailing slash / for adding path of files with $base_path as it adds a "/" to the beginning and end of the returned path.

  2. Use base_url if you want to get the full path: It will return base URL of the Drupal installation.

    Example:

    /*Assuming image path: http://localhost:81/my_website/image/header/headerR.png*/
    global $base_url; /* will return http://localhost:81/my_website in you case*/
    $image_path = $base_url . '/image/header/headerR.png';
    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.