Upload element

Alan D. - November 14, 2008 - 14:51
Example image and file upload fields in action

This module provides two new form elements to assist with files that are stored/updated in Drupal's {files} table.

Who should use it

This module is for developers who are building or modifying a form using Drupal's form API and are handling files that are stored natively using Drupal's core file management functionality.

It is not a CCK widget, Filefield is an excellent equivalent for CCK. The core upload module provides similar functionality on node forms.

Do not install this module unless you are a developer, an interested party, or directed to do so by another module.

Who should not use it

If you require to nest the element in a parent element (E.g: where $parent['#tree'] = TRUE ).
Ref: Fatal error when form['#tree'] = true

Features

Both elements share the following core features.

  • AHAH upload support.
  • Themable preview fields for the filename and image.
  • Maintains state with validation errors.
  • Works with public and private file system settings.
  • Works with and without clean URL support.
  • Temporary storage is based on the form build id, so multiple instances of the same form do not interact with each other.
  • Users get notifications about files that exceed the PHP based size limitations1.
  • Accepts validators to pass to file_save_upload. Image and max file size validators are added directly when applicable.
  • The required attribute works!

The image upload element has built in imagecache support when saving the newly uploaded file from the temp directory to it's permanent location. You also get control of the image preview size.

1 This excludes forms submitted using the main submit button that exceed the max post size. PHP drops the entire $_POST element, so Drupal will render a complete new form rather than an error on the original form.

What it doesn't do

It is simply designed to upload a file into Drupal's file directory with a record in Drupals {files} table.

  • This module does not provide functionality for presenting uploaded files or images.
  • It does not provide a hook_file_download for private file downloads.
  • It does not provide automated submit to save new files or to replace existing files.
  • It does not set the forms enctype. This patch should take care of this in core soon.

It also relies on Drupal's core file system handling to delete temporary files. As such, CRON must be enabled.

Usage

See the README.txt for details, but it is really as simple as:

<?php
  $form
['image'] = array(
   
'#type' => 'image_upload_element',
   
'#title' => t('Image'),
   
'#default_value' => $node->image, // {files} object
 
);

## And to handle the submission

  // in hook_insert or somewhere
  // $node->image is a {files} object
 
$image_id = 0;
  if(
$node->image) {
   
$image_id = upload_element_save($node->image, 'dest/directory', FILE_EXISTS_RENAME, 'imagecache_preset');
  } 
?>

PHP 4 and Devel users

You need to use version 6.x-1.2 or higher.

The future

Future planned enhancments are mainly internal and include;

  • Moving storage from the $_SESSION to the forms cache.
  • Maintaining a simple linked list to store an unlimited number of temperory uploads.
  • Creating a cleaner method of parsing the upload element from the cached form. Currently the module recursively searches for the element based on the element name and type.
  • Also considering to include the ability to rename the uploaded files, and to handle multiple file uploads.

For Drupal 7, I am considering forcing the ImageAPI as a requirement to handle the image thumbnails. This would allowing streaming of all thumbnail processing, preventing potential issues with writing to the tmp directory. Create an issue to discuss / vote on the merits of this move.

Lastly, if anyone is interested in helping out, drop me a line.

The latest DEV snapshot (committed 24 June to be packaged), addresses issues related to forms that are rebuilt, that break the AHAH uploads. Please test and give feedback.

Releases

Official releasesDateSizeLinksStatus
6.x-1.22009-Apr-0518.15 KBRecommended for 6.xThis is currently the recommended release for 6.x.
Development snapshotsDateSizeLinksStatus
6.x-1.x-dev2009-Jun-2418.37 KBDevelopment snapshotDevelopment snapshots are automatically regenerated and their contents can frequently change, so they are not recommended for production use.


 
 

Drupal is a registered trademark of Dries Buytaert.