function content_add_new_uploaded_file
Search API
7.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) |
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.
4 calls to content_add_new_uploaded_file()
- 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_handle_incoming_sms in modules/
engagements/ engagements.module - This catches incoming sms messages from POST, but can also be used by our "sms_get_all_messages" function, but it is also used by the sms_get_all_messages to save/update information.
- _engagements_imap_get_attachments in modules/
engagements/ engagements.module - $inbox is the imap link. email_number is msg_no.
File
- modules/
content/ content.module, line 1707
Code
function content_add_new_uploaded_file($file, $cid = 0, $bool_override_original_filename = TRUE, $attributes = CONTENT_PRIVATE_FILE, $bool_replace_exsting_filename = FALSE) {
// 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 = 'file__' . time() . '' . mt_rand(9, 9999) . '_' . $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_PUBLIC_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 file. Possibly because of permission issues on the destination directory,
the disk is full, or some other reason."), "error");
return;
}
$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");
}
}
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();
}