2012-03-13 62 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' 
); 
相關問題