2012-03-13 63 views
1

我有一个自定义模块,我想要一个字段上传图片。 Chrome似乎上传了该文件,但它不起作用,并且出现以下错误。有人可以请指点我正确的方向吗?为什么我的文件上传工作在Drupal 7中?

function nafa_adoption_form($form_state) 
{ 
    $form['#attributes'] = array('enctype' => "multipart/form-data"); 

    ... 

    $form['picture'] = array(
    '#type' => 'file', 
    '#title' => t('Picture'), 
    '#size' => 20, 
    '#upload_location' => 'public://uploads' 
); 

    $form['submit'] = array(
    '#type' => 'submit', 
    '#value' => t('Save'), 
); 




    return $form; 
} 

提交功能:

function nafa_adoption_form_submit($form, &$form_state) 
{ 


    dvm($form_state['values']); //field 'picture' is blank 

    $file = file_load($form_state['values']['picture']); 

    $file->status = FILE_STATUS_PERMANENT; 

    file_save($file); 

    $fileid = file_load($file); 

    variable_set('adoption_picture', $fileid->uri); 

    if ($file) 
    { 
    drupal_set_message("File uploaded "); 
    } 

    else 
    { 
    drupal_set_message("File could not be uploaded"); 
    } 

    drupal_set_message(t('Your form has been saved.')); 
} 

我也得到了以下错误:

Notice: Undefined property: stdClass::$uri in file_save() (line 573 of /home/amn7940/nafa.achristiansen.com/includes/file.inc). 

回答

1

如果你看一下docs for the file element type你会发现没有#upload_location财产。这是因为当使用file类型时,您需要将临时文件自己移动到永久位置。

从您在提交函数中使用的代码中,我确定您正在寻找managed_file类型。如果您使用该代码,则代码应该完美地开始工作:

$form['picture'] = array(
    '#type' => 'managed_file', 
    '#title' => t('Picture'), 
    '#size' => 20, 
    '#upload_location' => 'public://uploads' 
); 
相关问题