Drupal 7 file_save_upload returns false

I have a user form, with a field for the user to upload an image file (their logo). In the validate hook form, I have implemented file_save_upload, which constantly returns false. I see that the file is actually saved in the right place when loading, so why does file_save_upload not work?

Form field:

$form['company_logo'] = array( '#type' => 'managed_file', '#title' => t('Company Logo'), '#description' => t('Allowed extensions: gif png jpg jpeg'), '#upload_location' => 'public://uploads/', '#upload_validators' => array( 'file_validate_extensions' => array('gif png jpg jpeg'), // Pass the maximum file size in bytes //'file_validate_size' => array(MAX_FILE_SIZE*1024*1024), ), ); 

Verification Key:

  $file = file_save_upload( 'company_logo' , array(), 'public://uploads/', FILE_EXISTS_RENAME); if (!$file) { form_set_error('company_logo', t('Unable to access file or file is missing.')); } 
+4
source share
1 answer

The file managed item handles the downloaded file for you, so there is no need to file_save_upload() call file_save_upload() .

You get a NULL return because of these lines in file_save_upload() :

 // Make sure there an upload to process. if (empty($_FILES['files']['name'][$source])) { return NULL; } 

Since the file has already been processed, the function cannot do anything.

You can save the file entry by adding a submit handler to the form and using code similar to

 $file = file_load($form_state['values']['company_logo']); $file->status = FILE_STATUS_PERMANENT; file_save($file); 
+3
source

Source: https://habr.com/ru/post/1502215/


All Articles