2011-09-23 32 views
1

我在drupal中編寫自定義模塊。
的宗旨是:在drupal 6中,如何在多個步驟中處理uloaded文件?

1. Upload a csv file 
2. Display its content in a tabular layout. 
3. On confirmation, save it in database. 

問題,我面對:

  1. 我不能上傳任何文件。即使我上傳,我也沒有收到$ _FILES中的任何內容。 >>求助
  2. 如何拆分過程?假設我成功地上傳了文件[確實有你的幫助;]],並保存該文件,假設在drupal6/uploaded_data目錄中。如何重定向到下一頁,我可以從文件讀取並顯示錶格數據以供確認。

代碼:)
菜單鉤子和所有

function productsadmin_menu() { 
    $items['admin/settings/product-administration'] = array(
     'title' => 'Product Administration', 
     'description' => 'Upload products data', 
     'page callback' => 'productsadmin_form', 
     'access arguments' => array('access content'), 
     'type' => MENU_NORMAL_ITEM, 
    ); 
    return $items; 
} 

function productsadmin_form() { 
    return drupal_get_form('productsadmin_my_form'); 
} 

此功能被傳遞給drupal_get_form()

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

    $form['csv'] = array(
    '#type' => 'file', 
    '#title' => 'Product Catalog', 
    '#description' => 'Product catalog in specified csv format', 
    '#required' => FALSE, 
); 
    $form['submit'] = array(
    '#type' => 'submit', 
    '#value' => 'Submit', 
); 
    return $form; 
} 

驗證(這是不工作的註釋部分)

function productsadmin_my_form_validate($form, &$form_state) { 
    if($form_state['values']['csv'] == "") { 
     form_set_error('csv', t('Please input product catalog csv data')); 
    } 

/* // Check if file is uploaded (Not working) 
    if ($_FILES['files']['name']['csv'] == '') { 
     form_set_error('csv', t('Please upload product catalog' . $rahul_vals)); 
    } 
*/ 
} 

提交行動

function productsadmin_my_form_submit($form, &$form_state) { 
    /* 
     1. Move File to uploaded_dir 
     2. Change the header so that it is redirected to new page 
    */ 
} 

回答

2

在Drupal,你不應該使用$_FILES,使用

我做了這個例子Drupal的API爲您講解如何使用cvs

/** 
    * Form function 
    */ 
    function _form_cvs_import($form_state) { 
     $form['#attributes'] = array('enctype' => "multipart/form-data"); 
     $form['container'] = array(
     '#type' => 'fieldset', 
     '#title' => t('CVS UPLOAD') , 
    ); 
     $form['container']['cvs_file'] = array(
     '#type' => 'file' , 
     '#title' => t('CVS FILE') , 
     '#description' => t('insert your cvs file here') , 
    ) ; 
     $form['container']['submit'] = array(
     '#type' => 'submit' , 
     '#value' => t('SEND') , 
    ) ;  

     return $form ; 
    } 

/** 
* form validate 
*/ 
function _form_cvs_import_validate($form, $form_state) { 
$validators = array(
    'file_validate_extensions' => array('cvs'), 
); 
if(!file_save_upload('cvs_file', $validators)) { // the file is not submitted 
    form_set_error('cvs_file', 'Please select the cvs file') ; 
}else{ // the file is submitted another validation for extension 
    $file = file_save_upload('cvs_file', $validators, file_directory_path()) ; 
    if($file->filemime != 'application/octet-stream') { 
    form_set_error('cvs_file', 'Extensions Allowed : cvs') ;  
    }   
}  
} 


/** 
* form submit 
*/ 
function _form_cvs_import_submit($form, $form_state) { 
    $file = file_save_upload('cvs_file', $validators, file_directory_path()) ; // this is the cvs file in the tmp directory 
    $file_handler = fopen($file->filepath, 'r') ; // open this cvs file 
    $line_num = 0 ; 
    $fields = array() ; 
    while(!feof($file_handler)) { 
     $line_num++ ; 
     $line = fgets($file_handler) ; // this is the line/row 
     $line_array = explode(",", $line); // array of row fields 
     $field_num = 0 ; 
     foreach($line_array as $field) { 
      $field_num++ ; 
      $fields[$line_num][$field_num] = str_replace('"', '', $field); // E.g you can access second row and third field by $fields[2][3] 
     } 
    } 
    fclose($file_handler); 
    unlink($file->filepath); 
} 
+0

感謝名單的工作,你的答案解決第一個問題。 –

+1

如果你想在他提交頁面後重定向某個頁面,你可以在你的表單結構函數中使用 $ form ['#redirect'](例如我的例子中的_form_cvs_import)...例如,你希望用戶被重定向到雅虎.com提交表單後...你可以在你的表單中添加這一行... $ form ['#redirect'] ='http://yahoo.com'; ... 你是這個意思嗎 !!對不起,因爲我的英語不好...也許我把你的錯誤解釋爲 –

+0

Thanx的解決方案。還有一件事,如何在用戶提交表單後在同一頁面上添加「成功消息」。 –

相關問題