Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 268 Vote(s) - 3.42 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to get absolute path for a image in drupal?

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


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

#2
If Drupal is on its own domain (i.e., `http://example.com`), the absolute path would be `/sites/default/files/images/abc.jpg`.

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

------------------

As I explained in my answer on the same question you deleted, if you are trying to programmatically generate the path (that is, you do not know where the file directory is), you need to use [`file_directory_path()`][1]:

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

So if the files are in:

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

-----------------

Assuming your previous question is still the use-case, you should be using [`l()`][2] and [`theme_image()`][3] to generate the links and images, respectively, within your module. This will ensure the paths generated are correct given Drupal's environment.

Using the expected output you provided before, a solution would be:


// 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);

So, assuming your site is at `http://example.com/`, the output would be:

<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>

But if your site was in a subfolder (e.g. `http://example.com/drupal`), the output would be:

<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>


[1]:

[To see links please register here]

[2]:

[To see links please register here]

[3]:

[To see links please register here]

Reply

#3
I tried mark's solution and found out that my directory is
"/sites/default/files/images/a.jpg"

But when i write this into my nodes as img src, it didn't show related images.
Besides when i wrote
"../sites/default/files/images/a.jpg"
It worked :)

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

Reply

#4
Just use

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

In that case you will have a path like "http://www.mywebsite.com/sites/default/files/images/image.jpeg"

$uri is an internal path, like "public://images/image.jpeg"


This is Drupal 7, enjoy it, love it
Reply

#5
`public://` will give you the absolute path of the public folder

Example: to access

drupal/sites/default/files/images/

just put

public://images/
Reply

#6
For Drupal 7

$relativePath = "blabla/abc.jpg";

$uri = file_build_uri($relativePath);

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

$url = file_create_url($uri);

//show

[To see links please register here]

echo $url;
Reply

#7
file_directory_path has been removed in drupal 7. If you use it, you'll get an error 'call to undefined function.' Try to use drupal_real_path or <img src="../sites/default/files/pic1.jpg" />. You can find about the deprecated function here.

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through