Hi, I'm kinda newbi with drupal but currently I'm developing a module, I need that my module takes a file (a text file actualy) and then when the form that takes the file is submited the content of the file will be parsed, but I'dont know how to take the file submited for parsing from my module_form_submit method. If anyone can helpme I'll be thankfull

PD: Excuse my bad english I'm triying to improve it

Comments

snippet

Do you want to parse the file as a validation process?
or once you know the submit will be accepted & processed you want to read file's info?

<?php
function mymodule_nodeapi(&$node, $op, $arg = 0) {
  switch (
$op) {
    case
'insert': // no break;
   
case 'update':
      if (
$node->type == 'page') {
       
$node = (array)$node;
       
$info = $node['field_uploaded_file'][0];
       
$filepath = $info['filepath'];
        ...
// do your stuff here
    
}
  }
}
?>

Re: snippet

If you need to parse the file as soon as it gets uploaded as part of the preview process it would be a little more complicated.
You may leave a more descriptive use case to get better help.

PS: also note that in the previous snippet I used page content type and uploaded_file filefield names as example

thanks, well specifically

thanks, well specifically what my module needs to do is register a bulk of users into the database based on the file (that defines user and mail), so I only read the file and then I trow it. The thing is that I'm using de form api, so I need to process the file at the module_form_submit($form, &$form_state), I don't know where to put the code that you post (I'm newbie).

Thanks again

need more info

Do you need to upload the file?
I ask, because as a newbie I used to make things work first and later find out mistakes
(wait I still do that with unfamiliar APIs)

case 1) you need to install a module and the first time a set of users/mails/passwords should be imported (only once in a lifetime)

case 2) you really need to upload the file as an administrative task and import its content (user/mails/...)

anyhow,
why parse a file?
which format is it?
you should export your data as CSV (comma separated values)
and then import it with PHP functions for CSV

have you tried peeking existing modules?

existing module: countries_api

countries_api is a module which needs to import data "once in a lifetime", thus it has a CSV file for countries and country codes which it imports to DB on installation time

is that your case?

everything about CSV

you may find everything about CSV doing some research (inspecting code) drupal.org/project/modules?text=CSV

about the first snippet

by the way, the first snippet was about saving nodes with a cck filefield, so each node saved could be processed on nodeapi hook, by now you shouldn't know much about it, but some day you will
by now, it doesn't seems to be your case

Thanks, well the function

Thanks, well the function than I said is periodic so the thing of doing all at installation of the module doesn't apply in my case. The case is that I'm not gonna be the one that administer the site, I gonna hand it to a person that isn't precisely a computer engineer so I'm trying to do it with forms so he can upload the file and that's all. I'm gonna use CVS files, and I'm not really concerned on the parsing of the CVS file (since I know how to parse text).

I only need to do the things in the PHP old fashion way, that is my form has an action that points to a url in that url I take the file sended and I parse the content with the traditional PHP functions. My problems is that I don't know how to retrieve the file at my form_submit function (that is with the form api)

Thanks again for taking your time for replied me

Edit: I don't see your latest post earlier, I preffer don't use CCK beacuse I need that my aplication to be modular (and I don't know if I can export a cck content type), thanks

FAPI

so, you're using something like

<?php
$form
['upload'] = array(
 
'#type' => 'file',
  ...
);
?>

and you already have a working submit handler,
but the missing part is getting the file content,
right?

got lucky

as I told you before, doing some research will always reveal the path drupal.org/project/modules?text=CSV

but pointing right to the spot taxonomy_csv

there you will find a very tiny module which does exactly what you need, a bulk import from csv file, just for vocabularies, you will tweak it for user accounts instead

there are your answers like:

-- saving the uploaded file (also validating there was a file)

<?php
/**
* Handles CSV import form validation.
*/
function taxonomy_csv_import_validate($form, &$form_state) {
 
$form_state['upload_file'] = file_save_upload('upload');
  if (!
$form_state['upload_file']) {
   
form_set_error('upload', t('Please upload a file.'));
  }
}
?>

-- parsing the file and doing the bulk import (with a batch operation to avoid timeouts due to a large file)

<?php
/**
* Handles CSV import form submission.
*/
function taxonomy_csv_import_submit($form, &$form_state) {
  ...
 
$file = $form_state['upload_file'];
  ... 
 
$handle = fopen($file->filepath, 'r');
  ...
  while (
$line = fgetcsv($handle, 4096, $delimiter)) {
    ...
  }
  ...
}
?>

1- download taxonomy_csv
2- tweak it to your needs
3- and enjoy it!

Thanks, that's exactly what I

Thanks, that's exactly what I need, I'm very very thankful with you for taking the time to answer me, I'm gonna take a look at the module you suggest me.

nobody click here