I have a form created using the form API.
the form has 2 managed file upload options

$form['rate_table_logo'] = array(
		'#type' => 'managed_file',
		'#title' => t('Rate Table Logo:'),
		'#default_value' => variable_get('rate_table_logo', ''),
		'#upload_location' => 'public://rate_logo/',
		
	);
	$form[form_logo'] = array(
		'#type' => 'managed_file',
		'#title' => t('Mortgage Form Logo:'),
		'#default_value' => variable_get('form_logo', ''),
		'#upload_location' => 'public://form_logo/',
		
	);

how do I process the form field if the user used the file upload option?
how do I process the form field if the user did not use the file upload option?

I have

$validators = array();
$file = $form_state['values']['rate_table_logo'];
$file = file_save_upload('file', $validators, 'public://');

which does not seem to work

Comments

joshi.rohit100’s picture

Did you try this as I think it is handled by drupal. You don't need to do anything.

jesterFortyEight’s picture

Then how do i get the uploaded file path and fid?

Jaypan’s picture

$form_state['value']['rate_table_logo'] and $form_state['values']['form_logo'] will be equal to either 0 (zero - no file uploaded) or the fid of the uploaded file.

Note that at this point, the status of these files is FILE_STATUS_TEMPORARY, which means that the file will be deleted on the next cron run that comes six hours after the file was uploaded. You need to set the status to FILE_STATUS_PERMANENT. You can do this in your submit handler:

$file = file_load($form_state['values']['rate_table_logo']);
$file->status = FILE_STATUS_PERMANENT;
file_save($file);

You'll also need to add a 'usage' to the file, by calling file_usage_add(). And you'll also need to remove a 'usage' wherever the file may be deleted, by calling file_usage_delete().

jesterFortyEight’s picture

ok, i got the permanent process to work, but I am completely confused by the file_usage_add and file_usage_delete portion of your reply.

Jaypan’s picture

Drupal allows files to be reused. Each time a file is 'used' (that is to say, attached to some data), you need to add a 'usage'. You also need to add a usage remover, when that usage is done (for example when either the file, or the item it's attached to are deleted).

When you call file_delete() on a file, Drupal will check if there are still any registered usages for the file. If there are, then it doesn't delete the file. If there are no more usages, it will set the file to be deleted on a later cron run.

Because of this, you need to register a usage any time you add a file to the File API, or else you can get some hard to track bugs. And any time you register a usage, you need to ensure that your code also removes that usage somewhere else, to ensure that you don't end up with ghost files in your system.

jesterFortyEight’s picture

still not getting it

isn't the delete taken care of when you use the remove option of the managed file? whey do i need to code something to remove it?

Jaypan’s picture

the remove option of the managed file

What do you mean by this?

jesterFortyEight’s picture

when you have the manged file as a form entity and you upload a file it adds a remove button, doesn't this handle the delete process?

Jaypan’s picture

I don't believe it does, but I've been known to be wrong in the past. You should test to confirm.