function content_add_new_uploaded_file

7.x content.module content_add_new_uploaded_file($file, $cid = 0, $bool_override_original_filename = TRUE, $attributes = CONTENT_ENCRYPTED_FILE, $bool_replace_exsting_filename = FALSE, $filename_prefix = "file__")
6.x content.module content_add_new_uploaded_file($file, $cid = 0, $bool_override_original_filename = TRUE, $attributes = CONTENT_PRIVATE_FILE, $bool_replace_exsting_filename = FALSE)

bool_replace_existing means should we replace an existing file (based on filename) with this new one? Instead of creating an INSERT, that is.

$file is expected to be an array containing these elements: [ "name" (original filename) "type" (the mimetime) "tmp_name" (the system filepath to the file) ]

7 calls to content_add_new_uploaded_file()
content.module in modules/content/content.module
content_edit_content_form_submit in modules/content/content.module
Submit handler for the edit content form.
content_public_files_form_submit in modules/content/content.module
engagements.module in modules/engagements/engagements.module
This is the primary module file for the engagements module.
engagements_handle_incoming_mailgun_email in modules/engagements/engagements.module

... See full list

File

modules/content/content.module, line 1747

Code

function content_add_new_uploaded_file($file, $cid = 0, $bool_override_original_filename = TRUE, $attributes = CONTENT_ENCRYPTED_FILE, $bool_replace_exsting_filename = FALSE, $filename_prefix = "file__") {
  // Store the file information in the content_files table, and then THIS field contains the fid reference number.
  $original_filename = @$file ["name"];
  $type = @$file ["type"];
  $tmp_name = @$file ["tmp_name"];

  if (!strstr($tmp_name, "/")) {
    // Doesn't contain the temp location.  Pre-prend to the tmp_name.
    $tmp_name = sys_get_temp_dir() . "/" . $tmp_name;
  }

  // Figure out the extension of the original filename.
  $temp = explode(".", $original_filename);
  $ext = $temp [count($temp) - 1];

  $filename = $original_filename;
  if ($bool_override_original_filename) {
    $filename = $filename_prefix . time() . '' . mt_rand(9, 9999) . '_' . fp_get_machine_readable($original_filename);
  }

  // If the filename is too long, shorten it.  Linux won't allow more than 255 bytes (usually corresponds to chars, depending on file system
  // it might be as low as ), 
  // Windows its 260 chars.  Let's be safe and stop at 100 chars + ext.
  if (strlen($filename) > 100) {
    $filename = substr($filename, 0, 100) . ".$ext";
  }

  $extra = "";
  if ($attributes === CONTENT_PUBLIC_FILE) {
    // If this is a "public" file, then put in the correct public files dir.
    $extra .= "public_uploads/";
  }

  $full_filename = fp_get_files_path() . '/content_uploads/' . $extra . $filename;

  $is_encrypted = 0;
  // If we should be encrypting files, so do here, and set the is_encrypted value correctly.
  if (module_enabled('encryption') && variable_get("encryption_files_encryption", "yes") === 'yes' && encryption_get_key() && $attributes == CONTENT_ENCRYPTED_FILE) {
    // (note: public files are never encrypted)

    // We need to do that by loading the file into memory, then getting the encrypted version, then writing it
    // out to the destination.
    $file_contents = file_get_contents($tmp_name);
    $enc_file_contents = encryption_encrypt($file_contents);
    $full_filename .= ".enc";
    $filename .= ".enc";
    if (!file_put_contents($full_filename, $enc_file_contents)) {
      fp_add_message(t("Could not upload encrypted file.  Possibly because of permission issues on the destination directory,
                        the disk is full, or some other reason."), "error");

      watchdog('content', "Could not upload encrypted file.  Possibly because of permission issues on the destination directory,
                        the disk is full, or some other reason.", array(), WATCHDOG_ERROR);

      return FALSE;
    }

    $is_encrypted = 1;


  } // encryption module is enabled and we should be encrypting.
  else {
    // No encryption, just copy the normal way.

    $is_encrypted = 0;

    if (!copy($tmp_name, $full_filename)) {
      fp_add_message(t("Could not upload file.  Possibly because of permission issues on the destination directory,
                        the disk is full, or some other reason."), "error");

      watchdog('content', "Could not upload and copy file to destination.  Possibly because of permission issues on the destination directory,
                        the disk is full, or some other reason.", array(), WATCHDOG_ERROR);

      return FALSE;

    }

  }


  if ($bool_replace_exsting_filename) {
    $fid = db_result(db_query("SELECT fid FROM content_files 
                               WHERE filename = ?
                               AND cid = ?
                               AND `attributes` = ?", array($filename, $cid, $attributes)));
    if ($fid) {
      db_query("UPDATE content_files SET posted = ? WHERE fid = ?", array(time(), $fid));
      return $fid;
    }
    else {
      // File wasn't in there.  Just do nothing here and proceed to a normal insert.
    }
  }


  // Add to the content_files table.
  db_query("INSERT INTO content_files (cid, original_filename, filename, mimetype, is_encrypted, posted, `attributes`)
            VALUES (?, ?, ?, ?, ?, ?, ?)", array($cid, $original_filename, $filename, $type, $is_encrypted, time(), $attributes));
  return db_insert_id();

}