在drupal 6中,如何通过多个步骤处理uloaded文件?

发布于 2024-12-06 10:32:05 字数 2031 浏览 1 评论 0原文

我正在 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
    */
}

I am writing custom module in drupal.
Aim is to :

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

Problem I am facing:

  1. I am unable to upload any file. I am not getting any thing in $_FILES, even if I upload or not. >> SOLVED
  2. How do I split the process ? Suppose I succeed in uploading file [with your help indeed ;) ], and I save the file, suppose in drupal6/uploaded_data directory. How do I redirect to next page where I can read from file and show tabular data for confirmation.

Codes :)
menu hooks and all

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');
}

This function is passed to 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;
}

Validation (The part which is not working is commented)

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

Submit action

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

灰色世界里的红玫瑰 2024-12-13 10:32:05

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

you shouldn't use $_FILES in drupal,use drupal api

I made this example for you to explain how to work with 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);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文