Advertisement
Guest User

Untitled

a guest
Jul 10th, 2014
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. // uid of user that to add the file.
  2. $uid = $UID;
  3.  
  4. // Final file name
  5. $filename = $FILENAME;
  6.  
  7. // Fieldname where to attach the file
  8. $fieldname = $FIELDNAME;
  9.  
  10. // File temporary path.
  11. $file_tmp_path = $FILE_TMP_PATH;
  12.  
  13. // Let's go!
  14.  
  15. // Load user object.
  16. $account = user_load($uid);
  17.  
  18. // Gather file info.
  19. $filesize = filesize($file_tmp_path);
  20. $mime_type = mime_content_type($file_tmp_path);
  21.  
  22. // Create file object.
  23. $file = new stdClass;
  24. $file->uid = $uid;
  25. $file->filename = <desired filename>;
  26. $file->uri = $file_tmp_path;
  27. $file->filemime = $mime_type;
  28. $file->filesize = $filesize;
  29. $file->status = FILE_STATUS_PERMANENT;
  30.  
  31. $destination .= '/' . $filename;
  32.  
  33. // Move to its final destination and add it to the database.
  34. // file_move moves the file and is in charge of add the needed DB records.
  35. $file = file_move($file, $destination, FILE_EXISTS_RENAME);
  36.  
  37. // Attach file to user.
  38. $account->{$fieldname}[LANGUAGE_NONE][0]['fid'] = $file->fid;
  39.  
  40.  
  41. $user_save($account);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement