How to get the absolute path for an image in drupal?

I have some images in drupal / sites / default / files / images /.

How can I get the absolute path for an image such as abc.jpg placed in this directory?

+5
source share
6 answers

If Drupal is in its own domain (i.e. http://example.com), the absolute path will be /sites/default/files/images/abc.jpg.

If Drupal is in a subfolder (e.g. http://example.com/drupal), the absolute path will be /drupal/sites/default/files/images/abc.jpg.


As I explained in my answer on the same question that you deleted, if you are trying to programmatically generate a path (that is, you don't know where the file directory is), you need to use file_directory_path():

$image = file_directory_path() . "/images/abc.jpg";

, :

  • sites/default/files: $image = "sites/default/files/images/abc.jpg".
  • sites/example.com/files: $image = "sites/example.com/files/images/abc.jpg".

, - , l() theme_image() , , . , Drupal.

, , :

// Images are in drupalroot/sites/default/files/images
$path = file_directory_path() . '/images/';

$detail_file = 'detail_1.jpg';
$image_file  = '1.jpg';

$img = theme('image', $path . $image_file);

$options = array(
  'attributes' => array(
    'title' => t('Sample title for image 1 lorem ipsum'),
  ),
  'html' => TRUE,
);
$output = l($img, $path . $detail_file, $options);

, http://example.com/, :

<a href="/sites/default/files/images/default_1.jpg" title="Sample title for image 1 lorem ipsum">
  <img src="/sites/default/files/images/1.jpg" height="<image height>" width="<image width>" />
</a>

(, http://example.com/drupal), :

<a href="/drupal/sites/default/files/images/default_1.jpg" title="Sample title for image 1 lorem ipsum">
  <img src="/drupal/sites/default/files/images/1.jpg" height="<image height>" width="<image width>" />
</a>
+12

Drupal 7

$relativePath = "blabla/abc.jpg";

$uri = file_build_uri($relativePath);

//show public://blabla/abc.jpg
echo $uri; 

$url = file_create_url($uri);

//show http://www.yoursite.com/sites/default/files/blabla/abc.jpg
echo $url; 
+10

$url_image = url('sites/default/files/'.file_uri_target($uri), array('absolute'=>true));

, "http://www.mywebsite.com/sites/default/files/images/image.jpeg"

$uri - , "public://images/image.jpeg"

Drupal 7, ,

+4

file_directory_path drupal 7. , " undefined". drupal_real_path . . http://api.drupal.org/api/drupal/includes!file.inc/function/file_directory_path/6

+2

public://

:

drupal/sites/default/files/images/

public://images/
+1

, "/sites/default/files/images/a.jpg"

img src, . , "../sites/default/files/images/a.jpg" :)

Because in the first case, Drupal tried to find the file in "Content / sites / default / files / images / a.jpg"

0
source

All Articles