views:

1540

answers:

3

I am working on a module that takes a user-uploaded CSV file. Code looks like this:

function foo_form_submit($form_id, &$form_state) {
  $validators = array();
  $dest = 'sites/phoenix.dev/files';
  $uploaded_file = file_save_upload('upload', $validators, $dest);
//some other stuff
}

As you can see, I don't pass anything in to validate that the file in the 'upload' field is actually a .csv file. This would cause some nasty things to happen later on in the function. How do I use the validators to check that the extension is .csv, or even better, to check that it actually IS a .csv file?

Edit: and Google search did not turn up anything too helpful.

A: 

I don't see how you could check whether it's a valid CSV without actually trying to parse it and seeing if there are any errors.

Eli
I understand I can't check if the file is a valid CSV without parsing it. I want to check the extension of the file at least, just as some really basic validation. How can I validate the extension so only .csv files are uploaded?
Jergason
+1  A: 

Drupal documentation on file_validate_extensions suggests you want to change this:

$validators = array();

To this:

$validators = array( 'file_validate_extensions' => array( 'csv' ) );
Scott Reynen
A: 

True, it's not necessarily required to validate the contents of the file since the goal may be to stop (for example) .php scripts from executing, and with the extension being .csv apache wouldn't run this as a php script.

Charlie