Drupal 7 forms API: File upload

16,228

Solution 1

Doesn't answer the question of why the form doesn't provide with a file/url/... but it solves the whole upload issue:

function portal_upload_form_submit($form, &$form_state) {

    $file = $form_state['values']['file'];

    $validators = array();
    $file = file_save_upload('file', $validators, 'public://');

Solution 2

Here are a few other things to consider here:

  1. file_save_upload() only will validate a default list of content types: jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp

  2. Be sure to name your first item in an associative array format i.e. '#name' => 'files[img_1]',

file_save_upload() documentation, see the first comment for information on my second point.

Share:
16,228
Jem
Author by

Jem

Updated on June 04, 2022

Comments

  • Jem
    Jem almost 2 years

    I've been following the form example 10 from the Drupal doc: http://api.drupal.org/api/examples/form_example!form_example.module/group/form_example/7

    Here goes the definition of my form:

    function portal_upload_form($form, $form_state) {
    
        $form['file'] = array(
            '#type' => 'file',
            '#title' => t('Choose a file'),
        );
    
        $form['document_submit_button'] = array(
    
          '#type' => 'submit', 
          '#value' => t('upload'), 
        );
    
        return $form;
    }
    

    And the form_submit hook:

    function portal_upload_form_submit($form, &$form_state) {
    
        $file = $form_state['values']['file'];
            // ...
    

    $file is 'empty' despite having set an input file with 777 permissions. I'm missing something and can't find what on my own...

    Thanks! J.