User login

File uploads and Drupal 5 forms

sudo svn commit -m "uploading images will work so much better with the enctype set"

The important part, the lack of which is what was making image uploads simply not work for what were at the time unknown reasons, is the last line: $form['#attributes']['enctype'] = 'multipart/form-data';. For the file form element to work, for the file to be able to upload, the entire form's encoding type has to be set to multipart.

<?php
    $form['picture'] = array('#type' => 'fieldset', '#title' => t('Picture'), '#weight' => 1);

    $form['picture']['picture_upload'] = array(
      '#type' => 'file',
      '#title' => t('Upload picture'),
      '#size' => 48, '#description' => t('Your virtual face or picture. Maximum dimensions are %dimensions and the maximum size is %size kB.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'), '%size' => variable_get('user_picture_file_size', '30'))) .' '. variable_get('user_picture_guidelines', ''));

    $form['#attributes']['enctype'] = 'multipart/form-data';
?>

Here at Agaric, we can't possibly let things be easy.

drupal file uploads and multistep forms
php tell files to persist multipage form

Form field #type file and multistep forms, living in harmony -- myth or reality?

Not working for this guy:

Forms API + Multistep + File uploads problem
http://drupal.org/node/142401

Quoting Karoly Negyesi (chx) from the development list (11/16/07)

You have a problem here -- files are stored in FILES not in POST by PHP hence Form API does not deal with files at all -- form_values is a derived from POST.You need to handle uploads separately. Also, just populating FILES is not enough if I remember correctly PHP has an internal structure so an upload is not easily emulatable. You yourself will need to put the file on disk, save the entry into {files}... If you figure this out -- maybe with more help from the list -- please post to http://drupal.org/node/191154 -- this seems to be a popular question these days

Not a help for multistep: http://drupal.org/project/upapi

So I just moved the picture upload to the last step of the form. Because if at first you don't succeed, redefine success.

After wasting a ton of time of course.

Resolution

Searched words: 
FAPI2

Comments

Why SUDO?

Thanks for this nice post, it helped me pretty well.

sudo svn commit -m "uploading images will work so much better with the enctype set"

One question though, why are you commiting in SVN as a root? It makes things just complicated :p

Greetings

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.