2014-05-06 73 views
0

有誰知道我可以如何讓匿名用戶(基本上我的網站的所有訪問者都是匿名的,因爲我不處理用戶註冊)在我的Drupal 7網站上傳文件(例如.docx)?我需要一個特殊的Drupal模塊(無法找到任何)或PHP破解...在Drupal中匿名文件上傳?

回答

1

你可以寫一個塊的自定義模塊。該塊使用api格式呈現帶有文件上傳器的表單。 當表單被提交時(通過匿名用戶或授權),您可以將文件保存在公共文件系統中。這裏的形式代碼怎麼會看一個例子:

function mymodule_some_form($form, &$form_state) { 
    $form['some_file'] = array(
    '#type' => 'managed_file', 
    '#title' => t('A File'), 
    '#default_value' => variable_get('some_file'), 
    '#upload_location' => 'public://', 
); 

    return $form; 
} 

當表單提交,保存文件:

function mymodule_some_form_submit($form, &$form_state) { 
    $values = $form_state['values']; 

    // Save the uploaded file 
    if (isset($values['some_file']) && $values['some_file']) { 

    // Do the below to make it permanent otherwise 
    // the file will get deleted when cron runs 
    $file = file_load($values['healthcheck_image']); 
    file_usage_add((object)$file, 'mymodule', 'mymodule', 1); 

    if (!$file->status) { 
     $file->status = FILE_STATUS_PERMANENT; 
    } 

    file_save($file); 

    // Save file id to variables 
    variable_set('some_file', $file->fid); 
    } 
} 
+0

我不能很好地在Drupal開發精通。感謝代碼,但有一個易於使用的模塊嗎?我聽說WebForm模塊支持文件附件。總之,任何幫助,將不勝感激... – Asterix

+1

這沒關係!是的,webform模塊將會做你需要的。下載並啓用該模塊,然後創建一個webform節點。完成之後,您可以單擊新創建的節點上的'webform'選項卡,添加'文件'組件。這將允許用戶上傳文件。讓我知道你是否還有問題! – andyhails