Drupal 6 contains an upload.module which provides a UI for Drupal's built-in file.inc functions. It is possible to use file.inc functions directly without installing upload.module, but upload.module does provide a few useful features:
file.inconly associates file uploads with individual users,upload.modulewill also associate those files with nodes or even specific revisions of nodes. This association is stored in the{upload}table (previously called{file_revisions})upload.modulealso creates an extra fieldset on every node edit page allowing admins and/or users to manage the uploads for a given node.
There would be two approaches to attaching and viewing files programatically in Drupal:
- Replicating the functionality of upload.module but not the UI, allowing you to avoid enabling upload.module
- Enabling upload.module, but uploading files as non-listed to hide the upload.module UI
We will take the second approach here.
Step 1: Create a custom module which depends on upload.module
Create a module which includes the line
dependencies[] = upload
in it's .info file.
Step 2: Create an upload form
Step 3: Save the uploads
See upload_node_form_submit()
- $file = file_save_upload('form_field_key', $validators, file_directory_path())
- $node->files = array($fid => $file)
- $_SESSION['upload_files'][$fid] = $file
- upload_save($node)
Step 4: Display the uploads on the nodes
Useful upload.module API functions
Utility functions
- upload_space_used: wrapper for file_space_used which returns the amount of space used by the given uid
- upload_total_space_used: returns the amount of space used by all files from all users
Theme functions
- theme_upload_attachments: Outputs a table of files (used for the "listed" attachments in nodes)
- theme_upload_form_current: Outputs an upload form with a list of existing files included
- theme_upload_form_new: Outputs a fresh empty upload form
Nodeapi helper functions
- upload_load: called when a node is loaded
- upload_save: Given a node object with a files attribute, deletes files marked as "remove", and marks as FILE_STATUS_PERMANENT using file_set_status
- upload_delete: deletes all file attachments from a given node
- upload_delete_revision: deletes all file attachments from a given node for the current revision
Useful but not meant to be called
- _upload_form: used by both upload_js and upload_form_alter
- upload_form_alter: not useful to call directly but gives a good example of how to add upload form elements to a form
- upload_node_form_submit: form submit handler for upload_form_alter
- upload_js: handler for javascript uploads
