【问题标题】:In drupal 6, How to process uloaded file in more than one steps?在drupal 6中,如何通过多个步骤处理uloaded文件?
【发布时间】:2011-09-23 14:14:14
【问题描述】:

我正在用 drupal 编写自定义模块。
目标是:

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
    */
}

【问题讨论】:

    标签: php redirect file-upload drupal-6 http-headers


    【解决方案1】:

    你不应该在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);
    }
    

    【讨论】:

    • 如果你想在他提交页面后重定向某个人,你可以在你的表单结构函数中使用 $form['#redirect'] (在我的例子中是_fo​​rm_cvs_import)......例如你想要用户在提交表单后被重定向到 yahoo.com...您可以在您的表单功能中添加此行... $form['#redirect'] = 'yahoo.com'; ... 你是这个意思吗 !!对不起,因为我的英语不好......也许我理解错了
    • 感谢您的解决方案。还有一件事,如何在用户提交表单后在同一页面上添加“成功消息”。
    • 在您的表单提交功能 (_form_cvs_import_submit) 在我的示例中,您可以添加此... drupal_set_message("Successfully Uploaded") ; ...
    猜你喜欢
    • 2015-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2018-10-12
    • 1970-01-01
    • 2020-04-09
    • 1970-01-01
    相关资源
    最近更新 更多