1

I'm using the following code for validating and uploading a file from a form:

function request_form_submit($form, &$form_state) {
    $validators = array('file_validate_extensions' => array('png'));
    $file = file_save_upload('spectrum', $validators);
    if ($file) {
        //do stuff
    }

Uploading works, but I want to allow only png files to be uploaded. The validation part does not work at all, file uploading is always successful no matter which extension the uploaded file has.

I also tried giving a space separated list of extensions to file_validate_extensions as mentioned in the documentation, but that didn't change anything.

Any idea what I'm doing wrong here and why this doesn't work at all?

1 Answer 1

1
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['survey']['file-upload'] = array(
  '#type' => 'file',
  '#prefix' => '<div class="survey-file-upload">',
  '#suffix' => '</div></div>',
  '#description' => 'Image type PNG/JPG Max Size:2Mb.',
);

function create_survey_form_validate($form, &$form_state){
$save = file_save_upload('file-upload', array('file_validate_extensions' => array("jpg png")),"public://user_sheets",FILE_EXISTS_RENAME);
  if($save == null){
    form_set_error('file_upload', 'Please select a file');
  }
  else if($save == FALSE){
   form_set_error('file_upload', 'Error in file upload');
  }
}
1
  • Please add some explanation to your code.
    – Mołot
    Commented Apr 23, 2014 at 9:22

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.