0

I have created a block through custom module and added file field in block configuration form using below code.

function mymodule_block_configure($delta){
if($delta=='mymodule'){
    $form = array();
    $form['mymodule_file']=array(
        '#type'=>'file',
        '#title'=>'Upload text file',
        '#name'=>'files[sample]',

    );      
    return $form;
      }
 }

When I try to check this field in hook_block_save, the form element is empty even when the form is submitted after browsing the file.

 function mumodule_block_save($delta,$edit=array()){
    if($delta=='mymodule_block'){
    dpm($edit); 
    }
     }

$edit['mymodule_file'] is empty after the form is submitted.

Am I missing anything?

1 Answer 1

1

You want to use the #type 'managed_file' instead of just file: http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/7#managed_file.

That will then automatically save the file and create a row in {file_managed}, You just need to set it as permament, as explained in the link above and then save the fid, for example with variable_set().

You can also set the #default_value for the fid, and it will display the field again.

4
  • Thanks for the reply. But I do not want to save the file. I just need to set some variables depending on file content. How can I do that? Is it possible to read the file from temporary path? Also, how can I restrict the file type to text? My be my question title is misleading. Going to edit it now. Commented Sep 18, 2011 at 12:10
  • Ah, have a look at blog.ryantan.net/2011/04/drupal-7-handling-file-uploads then.
    – Berdir
    Commented Sep 18, 2011 at 13:01
  • thanks for that. i will give it a try. But still I am not sure if it is possible to read the uploaded file without saving it. Commented Sep 18, 2011 at 13:48
  • Have a look at the linked file_save_upload() function, there you can see where it gets the files from ($_FILES).
    – Berdir
    Commented Sep 18, 2011 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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