wesley tanaka

Uploading files to nodes programatically

(Drupal 6)

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.inc only associates file uploads with individual users, upload.module will 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.module also 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:

  1. Replicating the functionality of upload.module but not the UI, allowing you to avoid enabling upload.module
  2. 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()

  1. $file = file_save_upload('form_field_key', $validators, file_directory_path())
  2. $node->files = array($fid => $file)
  3. $_SESSION['upload_files'][$fid] = $file
  4. upload_save($node)

Step 4: Display the uploads on the nodes

Useful upload.module API functions

Utility functions

Theme functions

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

Syndicate content