0

I have a really simple form with a file upload. It actually used to work until I recently noticed it's not saving the file. When I check for the submitted values, the file field is empty, but the file is in $_FILES.

Here is how I build the form:

function tag_graphics_form($form_state) {
    $form = array();
    $form['tag'] = array(
        '#required' => TRUE,
        '#type' => 'textfield',
        '#title' => t('Tag'),
        '#size' => 30,
      );
    $form['image'] = array(
        '#type' => 'file',
        '#title' => t('Image for tag page'),
        '#description' => 'Recommended width for image: 605px'
    );
    $form['description'] = array(
        '#type' => 'textarea',
        '#title' => t('Page Description for SEO')
    );
    $form['#attributes'] = array('enctype' => "multipart/form-data");
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Save'
    );
    return $form;
}

And this is how I process things in the submit:

function tag_graphics_form_submit( $form, &$form_state ) {
    // save the image and put to the database
    print_r($form_state['values']);  // the image field is completely empty here. other fields are ok
    print_r($_FILES); // the uploaded file is here just fine
    if ($form_state['values']['image']) { // all of this used to work...
        $validators = array();
        $file = file_save_upload('image', $validators, file_directory_path());
        file_set_status($file, FILE_STATUS_PERMANENT);
        $filepath = $file->filepath;
    } 
    // more code...
}
0

1 Answer 1

1
function tag_graphics_form($form_state) {
    $form = array();
    $form['#attributes'] = array('enctype' => "multipart/form-data");
    $form['tag'] = array(
        '#required' => TRUE,
        '#type' => 'textfield',
        '#title' => t('Tag'),
        '#size' => 30,
      );
    $form['image'] = array(
        '#type' => 'file',
        '#title' => t('Image for tag page'),
        '#description' => 'Recommended width for image: 605px'
    );
    $form['description'] = array(
        '#type' => 'textarea',
        '#title' => t('Page Description for SEO')
    );

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Save'
    );
    return $form;
}

function tag_graphics_form_validate($form, &$form_state){
  $field = 'image';

  $directory = file_directory_path();
  if (file_check_directory($directory, FILE_CREATE_DIRECTORY, $field)) {
    $validators = array();
    if ($file = file_save_upload($field, $validators, $directory)) {
      file_set_status($file, FILE_STATUS_PERMANENT);
      $form_state['storage']['file'] = $file;

    }
  }
}
function tag_graphics_form_submit(&$form, &$form_state){
  print_r($form_state['storage']['file']);
}
1
  • @Moshe Nice to hear you :)
    – DRUPWAY
    Sep 18, 2014 at 9:54

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.