fileupload.module

  1. drupal
    1. 5

This is an example to demonstrate how to make a Drupal node support file uploads.

@Database definition:

<?php
  CREATE TABLE fileupload (
    nid int(10) unsigned NOT NULL default '0',
    filename varchar(255) not null,
    filepath varchar(255) not null,
    filemime varchar(255) not null,
    filesize int(10) unsigned not null
  );
?>

Functions & methods

NameDescription
fileupload_accessImplementation of hook_access.
fileupload_deleteImplementation of hook_delete().
fileupload_file_downloadImplementation of hook_file_download.
fileupload_formImplementation of hook_form().
fileupload_helpImplementation of hook_help.
fileupload_insertImplementation of hook_insert().
fileupload_loadImplementation of hook_load().
fileupload_menuImplementation of hook_menu.
fileupload_node_infoImplementation of hook_node_info.
fileupload_permImplementation of hook_perm.
fileupload_submitImplementation of hook_submit().
fileupload_updateImplementation of hook_update().
fileupload_validateImplementation of hook_validate().
fileupload_viewImplementation of hook_view.
View source
<?php

/**
 * @file
 * This is an example to demonstrate how to make a Drupal node support file
 * uploads.
 *
 * @Database definition:
 * @code
 *   CREATE TABLE fileupload (
 *     nid int(10) unsigned NOT NULL default '0',
 *     filename varchar(255) not null,
 *     filepath varchar(255) not null,
 *     filemime varchar(255) not null,
 *     filesize int(10) unsigned not null
 *   );
 * @endcode
 */

/**
 * Implementation of hook_menu.
 */
function fileupload_menu($may_cache) {
  $items = array();

  if ($may_cache) {
    $items[] = array('path' => 'node/add/fileupload', 'title' =>  t('file upload'),
      'access' => user_access('create files'));
  }

  return $items;
}

/**
 * Implementation of hook_perm.
 */
function fileupload_perm() {
  return array('create files');
}

/**
 * Implementation of hook_help.
 */
function fileupload_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('An example module to demonstrate file uploads.');
    case 'node/add#fileupload':
      return t('Simple file upload node example.');
  }
}

/**
 * Implementation of hook_node_info.
 */
function fileupload_node_info() {
  return array(
    'fileupload' => array(
      'name' => t('file upload'),
      'module' => 'fileupload',
      'description' => t('Example file upload content type'),
      'has_title' => TRUE,
      'has_body' => FALSE)
    );
}

/**
 * Implementation of hook_access.
 */
function fileupload_access($op, $node) {
  global $user;

  switch ($op) {
    case 'create':
      return user_access("create file");
    case 'update':
    case 'delete':
      return user_access("create file") && ($user->uid == $node->uid);
  }
}

/**
 * Implementation of hook_file_download.
 */
function fileupload_file_download($file) {
  if (user_access('access content')) {
    if ($filemime = db_result(db_query("SELECT filemime FROM {fileupload} WHERE filepath = '%s'", file_create_path($file)))) {
      return array('Content-type:' . $filemime);
    }
  }
  else {
    return -1;
  }
}

/**
 * Implementation of hook_view.
 */
function fileupload_view(&$node, $teaser = FALSE, $page = FALSE) {
  $node = node_prepare($node, $teaser);
  if ($file = $node->file) {
    // Only create link to file when it is in the proper location.
    if ($file->filesize && file_check_location($file->filepath, file_directory_path())) {
      $node->content['body']['#value'] = l($file->filename, file_create_url($file->filepath));
    }
    else {
      $node->content['body']['#value'] = $file->filename;
    }
    // display the file's size
    $node->content['body']['#value'] .= ' ('. format_size($file->filesize) .')';
  }

  return $node;
}

/**
 * Implementation of hook_form().
 *
 * Return an array of the form elements needed to edit this node.
 */
function fileupload_form(&$node) {
  // Set form parameters so we can accept file uploads.
  $form['#attributes'] = array('enctype' => 'multipart/form-data');
  // standard node title
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Title'),
    '#size' => 60,
    '#maxlength' => 128,
    '#required' => TRUE,
    '#default_value' => $node->title
  );
  // file upload field
  $form['file'] = array(
    '#type' => 'file',
    '#title' => t('File'),
    '#size' => 40,
    '#default_value' => '',
  );
  if ($node->file->filepath) {
      $form['file']['#description'] .= t('A file already exists, if you upload another file the current file will be replaced.');
  }

  if ($node->nid) {
    // the validate function doesn't get a full copy of the node, only the
    // data posted from the form. use this field to tell the validator
    // the path of the node's current file.
    $form['current_file'] = array(
      '#type' => 'value',
      '#value' => $node->file->filepath
    );
  }

  return $form;
}

/**
 * Implementation of hook_validate().
 *
 * Verify that there is a file attached to the node.
 */
function fileupload_validate(&$node) {
  // Check for a new file upload.
  if ($file = file_check_upload('file')) {
    $node->file = $file;
  }

  // Make sure there is an upload or an existing file.
  if (!$file && !file_exists($node->current_file)) {
    form_set_error('file', t('A file must be provided.'));
  }
}

/**
 * Implementation of hook_submit().
 *
 * If a file was uploaded save it before updating the database.
 */
function fileupload_submit(&$node) {
  // if a file was uploaded, move it to the files directory
  if ($file = file_check_upload('file')) {
    $node->file = file_save_upload($file, file_directory_path(), false);
  }
}

/**
 * Implementation of hook_load().
 *
 * The node is being loaded. Load the file's information from the database so
 * it can be added to the node.
 */
function fileupload_load($node) {
  $extras = new StdClass();
  $extras->file = db_fetch_object(db_query('SELECT filename, filepath, filemime, filesize FROM {fileupload} WHERE nid = %d', $node->nid));
  return $extras;
}

/**
 * Implementation of hook_insert().
 *
 * The node is being created, save the file's infomation to the database.
 */
function fileupload_insert($node) {
  $file = $node->file;
  db_query("INSERT INTO {fileupload} (nid, filename, filepath, filemime, filesize) VALUES (%d, '%s', '%s', '%s', %d)", $node->nid, $file->filename, $file->filepath, $file->filemime, $file->filesize);
}

/**
 * Implementation of hook_update().
 *
 * The node is being updated, save changes to the file infomation to the database.
 */
function fileupload_update($node) {
  $newfile = $node->file->filepath;
  $oldfile = db_result(db_query('SELECT filepath FROM {fileupload} WHERE nid = %d', $node->nid));

  // only make changes if there was a new file.
  if ($newfile) {
    if ($newfile != $oldfile) {
      // delete the old file
      file_delete($oldfile);
    }

    // update the database
    $file = $node->file;
    db_query("UPDATE {fileupload} SET filename = '%s', filepath = '%s', filemime = '%s', filesize = %d WHERE nid = %d", $file->filename, $file->filepath, $file->filemime, $file->filesize, $node->nid);
  }
}

/**
 * Implementation of hook_delete().
 *
 * The node is being delete, remove the file and any information.
 */
function fileupload_delete($node) {
  file_delete($node->filepath);
  db_query("DELETE FROM {fileupload} WHERE nid = %d", $node->nid);
}

?>