In a custom module, I created an admin form but I need an input to upload a csv file. When the file is uploaded I need to parse in and then I add data to db.

I already made "add to db" and "parsing" functions because when I installed the module, a default file is parsing and added to the db.



I'm using Drupal 7...

The data is stocked in a custom table that is create when the admin user will install the module. There is no node or content type. I just need to save all data of the CSV file (5 columns) in the table by an admin user form. 1 time par month the admin user need to update the table with a new .CSV.

In frontend I have a form and when the user will submit it I check that the code is in the table.

My issue is that I didn't know how to upload the file into a Drupal directory and then access to it after the form submit.

As I said above I already created a parsing function and I tested with a CSV file directly in the module directory, it's working. Data is insert into the table that I have created.

2 Answers 2


In your custom form create input button like

$form['upload_csv'] = array(
        '#name' => 'files[mycsv_data]',
        '#type' => 'file',
        '#title' => 'Upload csv'

Next in form submit you can use

                array('file_validate_extensions' => array('csv')),

Here for getting uploaded file details like file name and file path you can use $_FILES global variable.

and then,

if ($file) {
    $file->status = FILE_STATUS_PERMANENT;
  • 1
    According to your answer I added the code below to my hook_form_validate() and it's working. $file = file_save_upload('file', array( 'file_validate_extensions' => array('csv'), )); if($file) { $folder = "public://lampiris_smartmap/"; file_prepare_directory($folder, FILE_CREATE_DIRECTORY); file_create_htaccess($folder); if ($file = file_move($file, $folder)) { $form_state['storage']['file'] = $file; file_save($file); } else { form_set_error('file', t("Failed to write the uploaded file to the site's file folder.")); } }
    – Promo
    Commented Jul 13, 2015 at 13:14

use node import module or feeds import.

  • I forget to say that I'm working on Drupal 7.
    – Promo
    Commented Jul 13, 2015 at 10:18
  • drupal.org/project/feeds Commented Jul 13, 2015 at 10:22
  • So basically there is no way to get a file thru an admin form ?
    – Promo
    Commented Jul 13, 2015 at 10:46
  • Have you gone through this module? youtube.com/watch?v=59B2cRUGuCU This is the tutorial, it might help you. This module basically create a store admin menu, and in structure menu, there comes an option of feeds importer, where you define which type of nodes you want, which type of parser you want, choose csv over there, and create a raw csv format file from this importer. Edit your content in csv file and last import it via importer. Commented Jul 13, 2015 at 11:20
  • you can also follow this tutorial too. vimeo.com/22731881. It's so easy, just need to have a good understanding of this module. Either use feeds module or commerce feeds. Commented Jul 13, 2015 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.