You are here

function _upload_prepare in Drupal 5

Same name and namespace in other branches
  1. 4 modules/upload.module \_upload_prepare()

Save new uploads and attach them to the node object. append file_previews to the node object as well.

2 calls to _upload_prepare()
upload_js in modules/upload/upload.module
Menu-callback for JavaScript-based uploads.
upload_nodeapi in modules/upload/upload.module
Implementation of hook_nodeapi().

File

modules/upload/upload.module, line 288
File-handling and attaching files to nodes.

Code

function _upload_prepare(&$node) {

  // Clean up old file previews if a post didn't get the user to this page.
  // i.e. the user left the edit page, because they didn't want to upload anything.
  if (count($_POST) == 0) {
    if (is_array($_SESSION['file_previews']) && count($_SESSION['file_previews'])) {
      foreach ($_SESSION['file_previews'] as $fid => $file) {
        file_delete($file->filepath);
      }
      unset($_SESSION['file_previews']);
    }
  }

  // $_SESSION['file_current_upload'] tracks the fid of the file submitted this page request.
  // form_builder sets the value of file->list to 0 for checkboxes added to a form after
  // it has been submitted. Since unchecked checkboxes have no return value and do not
  // get a key in _POST form_builder has no way of knowing the difference between a check
  // box that wasn't present on the last form build, and a checkbox that is unchecked.
  unset($_SESSION['file_current_upload']);
  global $user;

  // Save new file uploads to tmp dir.
  if (($file = file_check_upload()) && user_access('upload files')) {

    // Scale image uploads.
    $file = _upload_image($file);
    $key = 'upload_' . count($_SESSION['file_previews']);
    $file->fid = $key;
    $file->source = $key;
    $file->list = variable_get('upload_list_default', 1);
    $_SESSION['file_previews'][$key] = $file;

    // Store the uploaded fid for this page request in case of submit without
    // preview or attach. See earlier notes.
    $_SESSION['file_current_upload'] = $key;
  }

  // Attach file previews to node object.
  if (is_array($_SESSION['file_previews']) && count($_SESSION['file_previews'])) {
    foreach ($_SESSION['file_previews'] as $fid => $file) {
      if ($user->uid != 1) {

        // Here something.php.pps becomes something.php_.pps
        $file->filename = upload_munge_filename($file->filename, NULL, 0);
        $file->description = $file->filename;
      }
      $node->files[$fid] = $file;
    }
  }
}