获取上传文件的路径

时间:2013-10-08 11:32:26

标签: file drupal-7

这是我上传照片的表格

$form['Background_image'] = array(
'#type' => 'managed_file',
'#title' => t('Choose a background image'),
 '#description' => t('Click "Browse..." to select an image to upload.'),
'#required' => TRUE,
'#upload_validators' => array('file_validate_extensions' => array('jpeg jpg png gif')),
'#upload_location' => 'public://backgroundimage/',
'#default_value' => $this->options['Background_image'],

);

我尝试了这个获取当前上传图片并返回其路径的功能,但仍无法正常工作:缺少的内容:

 function image_path()
   {
    $f = file_load($this->options['Background_image']);
     //this too is not working: 
     //$f = file_load($form_state['values']['Background_image']);
      $url_image = file_create_url($f->uri);
      print_r($url_image);
      return ($url_image);
    }

1 个答案:

答案 0 :(得分:0)

假设您的值$this->options['Background_image']是文件ID(a.k.a. fid),那么我在第二组代码中看到的唯一问题是您的print_r语句。 file_create_url返回一个字符串,而不是对象或数组,因此您只需打印输出。

所以替换:

print_r($url_image);

使用:

print $url_image;

它会将图片的完整网址(包括http)输出到浏览器。或者,如果要返回在另一个函数中使用的值,则只需删除print_r语句。