I have a custom module which allows a 'managed_file' upload in the settings/config page. This works fine and displays as expected after uploading (with the filename linked, and a "Remove" submit button) but the value of the field keeps disappearing after some time. What am I missing?
Here's the code from the custom module:
$form['custom_managed_jpg'] = array( '#type' => 'managed_file', '#name' => 'custom_managed_jpg', '#title' => 'Custom Image (JPG)', '#default_value' => variable_get('custom_managed_jpg', ''), '#upload_location' => 'public://custom_directory/', '#upload_validators' => array( 'file_validate_extensions' => array('jpg'), ), '#description' => t('This is a custom image (jpg format).'), );
It appears to be a problem with the file_load function. The variable is indeed saved, as I get a file ID when I
variable_get('custom_managed_jpg') (a number -- the
fid). However, when I
dpm() the result of
file_load($fid) it is empty.
The strangest part is that this works as expected to start with, and the file will even remain between page loads and cache clears, but then after a few hours it just disappears as described above without any warning.
It seems that the reason
file_load doesn't return anything is that the directory (
public://custom_directory/) is empty. At some point these files are being deleted from the directory but I have no idea why.
When I watch the
file_managed table, I can see a new file added after uploading. The one difference between files uploaded here and those already in the table is that the
status field is set to 0 instead of 1. I am guessing that could be part of the problem? How/where do I set the status to 1 programmatically?