0

I get "No file uploaded" when I try to use a custom form in drupal .Below is the code which I have used to upload a file in drupal custom form.

    function mobiae_custom_purchase_order_form(&$form,&$form_state) {
      // Merge default values into the default array.
        $form = array();
          $form['#attributes']['enctype'] = 'multipart/form-data';

          $form['file'] = array(
            '#type' => 'file',
            '#title' => t('Image'),
            '#description' => t('Upload a file, allowed extensions: jpg, jpeg, png, gif'),
          );




      return $form;
    }


function mobiae_custom_submit_form($payment_method, $pane_values, $checkout_pane, $order) {
  module_load_include('inc', 'mobiae_custom', 'mobiae_custom.purchase_order');
$form = array();
$form_state = array();
  // Default to a known test credit card number. For valid numbers of other card
  // types see: http://www.rimmkaufman.com/blog/credit-card-test-numbers/09112007/
//$form['#submit'][] = 'mobiae_custom_submit_form_validate';
  return mobiae_custom_purchase_order_form(&$form, &$form_state,$order); 
}

/**
 * Payment method callback: submit form validation.
 */
function mobiae_custom_submit_form_validate($payment_method, $pane_form, $pane_values, $order, $form_parents = array()) {
  // Validate the credit card fields.file_save
 drupal_set_message('fgh');
 $file = file_save_upload('file', array(
      'file_validate_is_image' => array(),
      'file_validate_extensions' => array('png gif jpg jpeg'),
  ));

  if ($file) {
    if ($file = file_move($file, 'public://')) {
      $pane_values['file'] = $file;
    }
    else {
      form_set_error('file', t('Failed to write the uploaded file the site\'s file folder.'));
    }
  }
  else {
    form_set_error('file', t('No file was uploaded.'));
  }

//  module_load_include('inc', 'commerce_payment', 'includes/commerce_payment.credit_card');
  if (form_get_errors()){
    return false;
  }
}
1

For saving file u can use

    $form['test_picture']['file'] = array(
        '#type' => 'managed_file',
        '#title' => t('Test Picture'),
        '#description' => t('Allowed extensions: gif png jpg jpeg'),            
        '#upload_location' => variable_get('picture_upload_location'),
        '#upload_validators' => array(
            'file_validate_extensions' => array('pdf'),
            'file_validate_size' => array(MAX_FILE_SIZE*1024*1024),
        ),
    );
1
  • Thanks for the answer , even after adding the above code, I am getting a "No file uploaded" error on submit of the form – harshal Jun 19 '15 at 7:58

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.