Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // uid of user that to add the file.
- $uid = $UID;
- // Final file name
- $filename = $FILENAME;
- // Fieldname where to attach the file
- $fieldname = $FIELDNAME;
- // File temporary path.
- $file_tmp_path = $FILE_TMP_PATH;
- // Let's go!
- // Load user object.
- $account = user_load($uid);
- // Gather file info.
- $filesize = filesize($file_tmp_path);
- $mime_type = mime_content_type($file_tmp_path);
- // Create file object.
- $file = new stdClass;
- $file->uid = $uid;
- $file->filename = <desired filename>;
- $file->uri = $file_tmp_path;
- $file->filemime = $mime_type;
- $file->filesize = $filesize;
- $file->status = FILE_STATUS_PERMANENT;
- $destination .= '/' . $filename;
- // Move to its final destination and add it to the database.
- // file_move moves the file and is in charge of add the needed DB records.
- $file = file_move($file, $destination, FILE_EXISTS_RENAME);
- // Attach file to user.
- $account->{$fieldname}[LANGUAGE_NONE][0]['fid'] = $file->fid;
- $user_save($account);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement