I used "enterprise blog" module for user blog postings. From external php file, I have to upload user picture programatically (i.e., for App). I have configured bootstrap perfectly and user_load($uid) is working, means drupal functions are working properly, but when I am trying to upload picture programaically "file_save($file)" is not working. Please check the below code and give me any suggestions to upload enterprise blog picture.

 $tmp_photo_realpath = "images/".$userimgname;
      $tmp_photo = "images/".$userimgname;
      $file = new stdClass();
      $file->uid      = $uid;
      $file->status   = 0; // mark the file as temporary
      $file->filename = basename($tmp_photo_realpath);
      $file->uri      = $tmp_photo;
     // $file->filemime = image_type_to_mime_type(exif_imagetype($tmp_photo_realpath));
      $file->filesize = filesize($tmp_photo_realpath);
     $infofile = explode("/",$tmp_photo);   
    $infoext = explode(".",end($infofile));
    $imgext = end($infoext);

  // The file should be an image
  $errors = array();
  $errors += file_validate_is_image($file);
  $errors += file_validate_image_resolution($file, variable_get('user_picture_dimensions', '100x100'));
  $errors += file_validate_size($file, variable_get('user_picture_file_size', '30') * 1024);

  // Make sure file extension is a valid image
  if (!in_array(strtolower($imgext), array('jpg', 'png', 'gif'))) {
    $errors[] = ' invalid image file extension.';

  if (count($errors)) {
    drupal_set_message(t('Profile Image Import:') . ' ' . $errors[0], 'warning');
    // Clean up (set fid to avoid error messages)
    $file->fid = 0; file_delete($file);
  else {
    // We'll need a valid file id on the file object; file_save() will give us one
    $file = file_save($file);
    // Update user account (fid is not empty, status is temporary -- image
    // will be moved to proper directory and assigned to the user)
    $fields['field_enterprise_blog_picture'] = $file;
    $account = user_save($account, $fields);
  • Do you really need to add a picture to User picture field or can be added to the image field(from Field)?
    – xurshid29
    Commented Apr 2, 2014 at 9:20
  • Image field which had a separate table as "field_data field_enterprise_blog_picture".
    – J Sinha
    Commented Apr 2, 2014 at 9:37

1 Answer 1


Ok, here is my testing example. I can't say that it works perfectly but in my case it works fine. And, of course, you can modify/customize it by your needs if you want (add validation, check file path, directories, and so on..):

function _executeFile($uri, $uid) {

  $file_url = file_create_url($uri);
  $file = system_retrieve_file($file_url, NULL, TRUE, FILE_EXISTS_REPLACE);

  if ($file) {
    $user = user_load($uid);
    $file = file_load($file->fid);
    $file->status = FILE_STATUS_PERMANENT;

    // file_usage_add($file, 'MY_MODULE', 'enterprize_picture', $file->fid);

    $file_info = image_get_info($file->uri);

    $user->field_enterprise_blog_picture[LANGUAGE_NONE][0]['fid'] = $file->fid;
    $user->field_enterprise_blog_picture[LANGUAGE_NONE][0]['alt'] = '';
    $user->field_enterprise_blog_picture[LANGUAGE_NONE][0]['title'] = '';
    $user->field_enterprise_blog_picture[LANGUAGE_NONE][0]['width'] = $file_info['width'];
    $user->field_enterprise_blog_picture[LANGUAGE_NONE][0]['height'] = $file_info['height'];
    $user->field_enterprise_blog_picture[LANGUAGE_NONE][0]['uid'] = $file->uid;
    $user->field_enterprise_blog_picture[LANGUAGE_NONE][0]['filename'] = $file->filename;
    $user->field_enterprise_blog_picture[LANGUAGE_NONE][0]['uri'] = $file->uri;
    $user->field_enterprise_blog_picture[LANGUAGE_NONE][0]['filemime'] = $file->filemime;
    $user->field_enterprise_blog_picture[LANGUAGE_NONE][0]['filesize'] = $file->filesize;
    $user->field_enterprise_blog_picture[LANGUAGE_NONE][0]['status'] = '1';
    $user->field_enterprise_blog_picture[LANGUAGE_NONE][0]['timestamp'] = $file->timestamp;
    $user->field_enterprise_blog_picture[LANGUAGE_NONE][0]['rdf_mapping'] = array();

  else {

and call it like this:

define('DRUPAL_ROOT', getcwd());
require_once './includes/bootstrap.inc';
require_once './includes/file.inc';
_executeFile($uri /* e.g.: 'public://OTHER-OpenSourceSystem_1600x1200.jpg' */, $uid);
  • First function gets two variables as arguments and loads user object and creates file object from URI, and sets file object properties to user file field and saves it;
  • Second piece of code bootstraps system and just calls that function.. You can set your validation rules here
  • Ok. I"ll try and let you know
    – J Sinha
    Commented Apr 2, 2014 at 10:02
  • Thank you. It's working in single trail. Now it's fine. Previously I struggled with this issue from few days..
    – J Sinha
    Commented Apr 2, 2014 at 13:03
  • @JSinha I forgot to mention that you also should add file usage to the added file with file_usage_add() function. It prevents deletion of that file by others.. I'll add it to my answer...
    – xurshid29
    Commented Apr 2, 2014 at 13:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.