How to upload enterprise blog picture programatically in drupal 7 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-&gt;uid      = $uid;
      $file-&gt;status   = 0; // mark the file as temporary
      $file-&gt;filename = basename($tmp_photo_realpath);
      $file-&gt;uri      = $tmp_photo;
     // $file-&gt;filemime = image_type_to_mime_type(exif_imagetype($tmp_photo_realpath));
      $file-&gt;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-&gt;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);
}
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-&gt;fid);
    $file-&gt;status = FILE_STATUS_PERMANENT;
    file_save($file);

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

    $file_info = image_get_info($file-&gt;uri);

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

    user_save($user);
  }
  else {
    // SET VALIDATION ERROR
  }
}


and call it like this:

define('DRUPAL_ROOT', getcwd());
require_once './includes/bootstrap.inc';
require_once './includes/file.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
_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