Uploaded images in theme settings is going to be lost I have a flexslider and I make its images customizable through the theme settings. I added a caption property for the the slide to meet multiple languages websites as follows in theme-settings.php:

// setting property to determine the rendered images fields 
$form['best_responsive_settings']['imagesCount'] = array(
      '#markup' =&gt; t('Enter the total number of images in the slideshow'),
      '#type' =&gt; 'select',
      '#options' =&gt; associate_array_vals(explode(',',  theme_get_setting('imagesCountOptions'))),
      '#selected' =&gt; theme_get_setting('imagesCount'),
      '#default_value' =&gt; theme_get_setting('imagesCount'),

  );

  $lang_list = language_list();
// rendering upload image form with caption textfield for every language supported by the website

  for ($i = 0; $i &lt; theme_get_setting('imagesCount'); $i++){
      $form['best_responsive_settings']['slideshowImages'.$i] = array(
        '#type' =&gt; 'managed_file',
        '#title' =&gt; t('Image').' #'.($i+1),
         '#required' =&gt; false,
        '#upload_location' =&gt; file_default_scheme() . '://slides',
    '#default_value' =&gt; theme_get_setting('slideshowImages'.$i,'best_responsive'), 
    '#upload_validators' =&gt; array(
      'file_validate_extensions' =&gt; array('gif png jpg jpeg'),
      'file_validate_image_resolution' =&gt; array('960x400','430x400')        ),

      );
      foreach ($lang_list as $lang){
      $form['best_responsive_settings']['slideshowCaption'.$lang-&gt;language.$i] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Caption').' #'.($i+1).' -- '.$lang-&gt;name,  
    '#required' =&gt; false,
    '#default_value' =&gt; theme_get_setting('slideshowCaption'.$lang-&gt;language.$i, 'best_responsive'),
  );
      }
  } 


In page.tpl.php I use the following to render the images:

&lt;?php if (theme_get_setting('slideshow_display','best_responsive')): ?&gt; 

    &lt;div id="home-slider"&gt;
      &lt;div class="flexslider-container"&gt;
        &lt;div id="single-post-slider" class="flexslider"&gt;
          &lt;ul class="slides"&gt;
            &lt;?php for ($i = 0; $i &lt; theme_get_setting('imagesCount'); $i++): ?&gt;
              &lt;?php  if (theme_get_setting('slideshowImages'.$i) != ''): ?&gt;
 //the following line causes a problem after some time
            &lt;li class="slide"&gt;&lt;img src="&lt;?php echo file_create_url(file_load(theme_get_setting('slideshowImages'.$i))-&gt;uri);?&gt;" alt="Slide"/&gt;&lt;?php if (theme_get_setting('slideshowCaption'.$language-&gt;language.$i)): ?&gt;&lt;span class="flex-caption"&gt;&lt;?php echo theme_get_setting('slideshowCaption'.$language-&gt;language.$i) ;?&gt;&lt;/span&gt;&lt;?php endif;?&gt;&lt;/li&gt;

              &lt;?php endif; ?&gt;
            &lt;?php endfor; ?&gt;
          &lt;/ul&gt;&lt;!-- /slides --&gt;
        &lt;/div&gt;&lt;!-- /flexslider --&gt;
      &lt;/div&gt;
    &lt;/div&gt;
    &lt;?php endif; ?&gt;
    &lt;?php endif; ?&gt;


Everything, initially, works fine, the images are uploaded to the theme settings and images are rendered well in the slideshow. However, after some time! I noticed that the images are disappeared from the template settings page and only the caption if supplied is remained. In addition I got the following notice:

Notice: Trying to get property of non-object in include() (line 159 of C:\xampp-2\htdocs\4test\drupal-7.28\sites\all\themes\best_responsive\templates\page.tpl.php).


Where the line 159 is the line regarded in the comment of the second code snippet above. This notice is repeated twice. What is the problem here? What is making this code initially works then after sometime it fails? What is making the uploaded files disappeared from the settings. In addition, the destination in-which images should be uploaded /sites/default/files/slides becomes empty. i.e the uploaded images files are deleted!
My solution is to ensure to make permanent the image when drupal load page theme settings. You don't need to create a custom submit handler because after the new settings are saved the settings page is reloaded, and the fix executed.

Plus, you cannot use "active theme" because sometimes the current active theme is the admin theme, and not the default or maybe the user is changing the settings theme in a theme that it's not set as default.

function YOUR_THEME_form_system_theme_settings_alter(&amp;$form, &amp;$form_state)  {

  $default_file_dir = 'public://template/logos';
  $folder = file_prepare_directory($default_file_dir, FILE_CREATE_DIRECTORY);
  $settings_theme = $form_state['build_info']['args'][0];

  if ($folder) {
    $my_logo = theme_get_setting('my_logo');
    // BUG: Force file to be permanent.
    if (!empty($my_logo)) {
      _fix_permanent_image('my_logo', $settings_theme);
    }
    $form['my_logo'] = array(
      '#type'               =&gt; 'managed_file',
      '#title'              =&gt; l('My logo'),
      '#default_value'      =&gt; $my_logo,
      '#progress_message'   =&gt; t('Please wait...'),
      '#progress_indicator' =&gt; 'bar',
      '#upload_location'    =&gt; $default_file_dir,
      '#upload_validators'  =&gt; array(
        'file_validate_extensions' =&gt; array('gif png jpg jpeg'),
        'file_validate_image_resolution' =&gt; array('375x50', '30x50'),
      ),
    );
  }

}

function _fix_permanent_image($image, $theme) {
  $fid = theme_get_setting($image, $theme);
  if ($fid &gt; 0) {
    $file = file_load($fid);
    if (is_object($file) &amp;&amp; $file-&gt;status == 0) {
      $file-&gt;status = FILE_STATUS_PERMANENT;
      file_save($file);
      file_usage_add($file, $theme, 'theme', 1);
      drupal_set_message($image . ' saved permanently.', 'status');
    }
  }
}