如何在drupal中获取图像的绝对路径?

发布于 2024-09-15 05:16:54 字数 94 浏览 4 评论 0原文

我在 drupal/sites/default/files/images/ 中有一些图像。

如何获取放置在该目录中的像 abc.jpg 这样的图像的绝对路径?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

雨的味道风的声音 2024-09-22 05:16:55

file_directory_path 已在 drupal 7 中删除。如果您使用它,您将收到错误“调用未定义的函数”。尝试使用 drupal_real_path 或 .您可以在此处找到有关已弃用功能的信息。 http://api.drupal.org/api/drupal /includes!file.inc/function/file_directory_path/6

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 . You can find about the deprecated function here. http://api.drupal.org/api/drupal/includes!file.inc/function/file_directory_path/6

—━☆沉默づ 2024-09-22 05:16:55

public:// 将为您提供公共文件夹的绝对路径

示例:访问

drupal/sites/default/files/images/

刚刚放置的

public://images/

public:// will give you the absolute path of the public folder

Example: to access

drupal/sites/default/files/images/

just put

public://images/
猥琐帝 2024-09-22 05:16:55

我尝试了马克的解决方案,发现我的目录是
“/sites/default/files/images/a.jpg”

但是当我将其作为 img src 写入节点时,它没有显示相关图像。
另外当我写的时候
“../sites/default/files/images/a.jpg”
它起作用了:)

因为在第一种情况下,Drupal 试图在中查找文件
“内容/站点/默认/文件/图像/a.jpg”

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"

难理解 2024-09-22 05:16:54

如果 Drupal 位于其自己的域上(即 http://example.com),则绝对路径将为 /sites/default/files/images/abc.jpg

如果 Drupal 位于子文件夹中(例如 http://example.com/drupal),则绝对路径将为 /drupal/sites/default/files/images/abc.jpg.


正如我在回答您删除的同一问题时所解释的那样,如果您尝试以编程方式生成路径(也就是说,您不知道文件目录在哪里),则需要使用 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>

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():

$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() and theme_image() 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>
寄意 2024-09-22 05:16:54

对于 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; 

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 http://www.yoursite.com/sites/default/files/blabla/abc.jpg
echo $url; 
命比纸薄 2024-09-22 05:16:54

只需使用

$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,享受它,喜欢它

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文