2012-04-05 33 views
1

我在Drupal上傳路徑時遇到了問題,當在一個多語言網站。 我已經制作了一種新語言,並且由於該圖片的源代碼沒有反斜槓,因此無法使用。Drupal上傳多語言網站的路徑

我的函數使用下面的代碼。

variable_get('file_public_path', drupal_get_path('theme', $theme) . '/uploads').'/'; 

如果我使用下面的一個,它就會起作用。

'/' . variable_get('file_public_path', drupal_get_path('theme', $theme) . '/uploads').'/'; 

這是一種醜陋的解決方案,我敢打賭,有一些更優雅的方式來處理這個問題。

回答

1

替代您的代碼,以獲取保存在公共路徑中的圖像的URL是以下代碼。 (替換「image.png」用正確的文件名)。

$image_url = file_create_url(variable_get('file_public_path', conf_path() . '/files') . '/image.png'); 

在你的代碼,你需要使用斜槓開頭,否則路徑不被認爲是相對於服務器的文檔目錄。
假設您啓用意大利語作爲Drupal站點的語言,頁面的URL爲http://example.com/it/page,並且圖像保存在sites/default/files/image.png中。如果沒有斜線,圖片網址會被瀏覽器視爲http://example.com/it/sites/default/files/image.png;使用斜槓,圖像URL從瀏覽器被認爲是http://example.com/sites/default/files/image.png

另外,「file_public_path」Drupal變量的默認值應該是variable_get('file_public_path', conf_path() . '/files'。這是system_file_system_settings()中使用的默認值。

$form['file_public_path'] = array(
    '#type' => 'textfield', 
    '#title' => t('Public file system path'), 
    '#default_value' => variable_get('file_public_path', conf_path() . '/files'), 
    '#maxlength' => 255, 
    '#description' => t('A local file system path where public files will be stored. This directory must exist and be writable by Drupal. This directory must be relative to the Drupal installation directory and be accessible over the web.'), 
    '#after_build' => array('system_check_directory'), 
);