Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6617461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:42:27+00:00 2026-05-25T20:42:27+00:00

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

  • 0

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
    */
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:42:28+00:00Added an answer on May 25, 2026 at 8:42 pm

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a Drupal module to integrate with a custom Java-based REST API for
I'm writing a custom module and I'd like to use $form_state of the current
I'm writing a custom file selection component. In my UI, first the user clicks
I am writing a custom c# HttpModule that will handle requests from all file
I'm writing a custom module and I am trying to create an array of
I have a custom module I'm writing, part of what I want it to
I'm writing a Drupal module and have run into what should be a trivial
I'm writing a custom module for one of my projects and I'm having a
I'm writing a custom Orchard module with a custom Theme. I would like to
When it comes to writing custom MySQL database-driven PHP session management for a VERY

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.