2

I have been trying for a week to get image files uploaded to a Drupal server through the use of "managed_file" theme settings to remain on the site longer than the default 6 hours. I have visited the Stack Overflow/Exchange forums before (https://stackoverflow.com/questions/8793518/set-file-status-permanent-for-appearance-settings-file-upload) (Uploaded images in theme settings is going to be lost) and have done my best to implement the provided solutions, but the uploaded files always disappear after 6 hours.

Here is one of the managed_file fields which I wish to make permanent:

$form['mentor_settings']['global_settings']['header_settings']['header_background_image'] = array(
      '#type' => 'managed_file',
      '#title' => t('Header Background Image'),
      '#default_value' => theme_get_setting('header_background_image'),
      '#description' => t('This image will match the full dimensions of the header in the back.'),
      '#upload_location' => 'public://themesettingspictures',
      '#upload_validators' => array(
        'file_validate_extensions' => array("png jpg jpeg gif"),
             ),
  );

The file is properly moved to the specified folder. I have attempted to set the file status to permanent using the code provided in the above links, but it has never worked.

I would like to know if anyone can show me what function will set the file status for me AND can show me how to write it for the setting above, with its very long array "path". The examples I have tried to imitate have always been for 1st level theme settings, but my theme has three different levels of settings, so I'm never sure if I'm referencing my individual settings properly when trying to change file status.

One more note: I have been a self-taught front end Wordpress and Drupal web designer for a year now, but my knowledge of PHP is weak and so I am usually unable to code and back-end functions on my own. If you simply tell me to "use" a certain PHP function, I will have no idea how to apply it within the context of Drupal. If you respond to my post with references to PHP functions, please explain if they must be in a certain file (theme-setting.php vs template.php) and provide any other Drupal contextual information.

2

You need set status FILE_STATUS_PERMANENT file when form submit

function mymodule_custom_form_submit($form, &$form_state) {
  if (isset($form_state['values']['images'])) {
    $file = file_load($form_state['values']['images']);

    $file->status = FILE_STATUS_PERMANENT;

    file_save($file);
  }
}
1

Drupal deletes on cron runs any files that are not in usage. So basically only files in file or e.g. image fields will remain in the system.

But there is a module for that:

https://www.drupal.org/project/file_lock

All newly uploaded files will be in usage by the file lock module than.

  • Tried the module out. Has not affected the permanency if file uploads as far as I can tell. My theme settings files still disappear after 6 hours. – isampso1 Mar 30 '15 at 19:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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