1

How can I get the right value to use in $file = file_save_upload($public_file, $validators, 'public://', FILE_EXISTS_REPLACE);

$public_file = $form_state['input']['files']['public_file'];

This upload field comes in the form via an ajax request so it comes in $form_state['input']['files']['public_file'] according to DSM. But when I do this it doesn't work.

I only got this function to work when the public_file is in the base of a form $form['public_file'] and entering public_file in the first value of file_save_upload.

But that isn't possible this way because of the ajax generated form.

If anyone knows how to get this value I would be very thankful. My code is below:

function push_notifications_edit_app_form_submit($form, &$form_state) {
    $title = $form_state['input']['title']['changethis'];
    $app_id = $form_state['input']['edit']['app_id'];
    $gcm = $form_state['input']['edit']['GCM'];
    // $ios = $form_state['input']['edit']['iOS'];
    // $ios = $form_state['input']['hidden_ios'];
    $oldid = $form_state['input']['oldid'];
    $ios_mode = $form_state['input']['edit']['ios_mode'];
    $public_file = $form_state['input']['files']['public_file'];

    $validators = array('file_validate_extensions' => array('pem'));
    $file = file_save_upload($public_file, $validators, 'public://', FILE_EXISTS_REPLACE);
    $ios = $file->filename;
    if ($file) {
        drupal_set_message(t('Thank you for uploading private file. You can download it from @url',
        array('@url' => file_create_url($file->uri))));
    }
    $query = db_update('push_notifications_api')
        ->fields(array(
               'app_id' => $app_id,
               'GCM' => $gcm,
               'ios' => 'hoi',
               'ios_mode' => $ios_mode,
            ))
        ->condition('app_id', $oldid)
        ->execute();
    drupal_set_message(t('@title has been updated succesfully.', array('@title' => $title))); 
    dsm($form_state);
}

And in the _form function:

$form['#attributes']['enctype'] = 'multipart/form-data';

$form['replace_textfield']['public_file'] = array(
    '#type' => 'file',
    '#title' => t('iOS Certificate File (.pem)'),
    '#description' => t('Upload your iOS certificate file here. Currently you are using: <strong>@cert</strong>', array('@cert' => $ios_key)),
    '#weight' => 35,
);

$form['replace_textfield']['edit']['ios_mode'] = array(
    '#type' => 'select',
    '#title' => 'APNS Environment',
    '#description' => 'Select the active APNS Environment. Please note that development certificates do not work with apps released in the Apple app store; production certificates only work with apps released in the app store.',
    '#options' => array(
        'prod' => 'Production',
        'dev' => 'Development',
    ),
    '#weight' => 40,
    '#value' => $ios_mode,
);

$form['replace_textfield']['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Edit',
    '#weight' => 40,
);
2
  • Your post is vague. Try editing it and explicitly state your workaround. Apr 23, 2015 at 9:27
  • I just need the file_save_upload to work. It works when the file type is in the form like: $form['public_file'] and when I use "public_file" in file_save_upload. But I need it to work when my ['public_file'] is in a form like $form['replace_textfield']['public_file']. Apr 23, 2015 at 9:29

0

Your Answer

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

Browse other questions tagged or ask your own question.