Check file extension

No replies
Last seen: 6 years 51 weeks ago
Joined: 2012-04-10

EDIT : It was easier than i thought. #upload_validators is mor appropriate than #element_validate for my issue. So the following code do what i wanted to do

function csv2user_form($form, &$form_submit) {
  $form['file'] = array(
    '#title' => 'Choisissez votre fichier CSV',
    '#type' => 'managed_file',
    '#required' => TRUE,
    '#upload_validators' => array(
      'file_validate_extensions' => array('csv'),
    '#description' => 'fichier du type nom_du_fichier.csv',
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Valider',
  return $form;


I have watched the videos of this website and found it very helpfull. Now I have try to build my first module and i have some troubles.
I have to build a module to upload CSV files and do some things with this files.
But i don't understand how to check the extension of the file.

I have created an input with this code

$form['file'] = array(
    '#title' => 'Choisissez votre fichier CSV',
    '#type' => 'managed_file',
    '#required' => TRUE,
    '#element_validate' => array('csv2user_validate'),
    '#description' => 'fichier du type nom_du_fichier.csv',

I have tried to use the #element_validate and i have created a function like this

function csv2user_validate($element, &$form_state) {
  $fid = $form_state['values']['file']['fid'];
  $file = file_load($fid);
  if ($file != null) {
    $error=file_validate_extensions($file, "csv");
    if ($error) form_set_error('file','Vous devez uploader un fichier csv');

But when i upload a csv file i've got this error The specified file aa.csv could not be uploaded. Only files with the following extensions are allowed: jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp.
And when i upload another file type like png i've got my message error Vous devez uploader un fichier csv but it doesnt prevent the file to be uploaded.

If somebody could help me it would be very nice.

Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's easy to become a member

Add to cart:

Contact us

Below are the supported keyboard shortcuts for our video player.

Start / stop video
Go to next video
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
Jump to a percentage (2 is 20%, for example)
Toggle full screen
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
1 month
6 months
10% off
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.