【问题标题】:how to get absolute path for a image in drupal?如何在drupal中获取图像的绝对路径?
【发布时间】:2010-08-19 08:07:13
【问题描述】:

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

如何获取此目录中的 abc.jpg 等图像的绝对路径?

【问题讨论】:

    标签: drupal absolute-path


    【解决方案1】:

    如果 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";
    

    所以如果文件在:

    • 网站/默认/文件$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>
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        【解决方案3】:

        随便用

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

          【讨论】:

            【解决方案5】:

            public:// 会给你公用文件夹的绝对路径

            示例:访问

            drupal/sites/default/files/images/
            

            随便放

            public://images/
            

            【讨论】:

              【解决方案6】:

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

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

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

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2021-02-09
                • 1970-01-01
                • 2019-07-29
                • 1970-01-01
                • 1970-01-01
                • 2011-02-07
                • 2023-03-31
                • 2011-12-02
                相关资源
                最近更新 更多