File Upload visible in $_FILES but drupal file functions create empty file I created a custom form with a file upload field:

$form['csvfile'] = array(
    '#title' =&gt; 'CSV File',
    '#type' =&gt; 'file',
);


When I submit the form, I can see the correct information in the $_FILES array:



However, when I try to use the code in the examples module to get the contents of that file and write it to an unmanaged file, the file is empty.



So, the resulting file is 0 bytes long. 

How do I access the uploaded file in a drupal-y way? I know I can just use the basic PHP functions to access the file, but I'm trying to learn how to do it the way drupal intends.

My submit handler code is almost entirely lifted from the examples module.

function herdapi_form_bulkupload($form, &amp;$form_state) {
    $form = array();
    $form['email'] = array(
        '#title' =&gt; 'Owner Email',
        '#type' =&gt; 'textfield',
    );

    $form['password'] = array(
        '#title' =&gt; 'Password',
        '#type' =&gt; 'password',
    );

    $form['csvfile'] = array(
        '#title' =&gt; 'CSV File',
        '#type' =&gt; 'file',
    );

    $form['submit'] = array(
        '#value' =&gt; 'Upload',
        '#type' =&gt; 'submit',
    );

    $form_state['rebuild'] = TRUE;
    $form_state['redirect'] = FALSE;

    return $form;
}

function mymodule_form_bulkupload_submit($form, &amp;$form_state) {

    dsm($form_state);
    dsm($_FILES);
    $data = $form_state['values']['csvfile'];

    // With the unmanaged file we just get a filename back.
    $filename = file_unmanaged_save_data($data, NULL, FILE_EXISTS_REPLACE);
    if ($filename) {
        $url = file_create_url($filename);
        $_SESSION['file_example_default_file'] = $filename;
        drupal_set_message(
            t('Saved file as %filename (accessible via !url, uri=&lt;span id="uri"&gt;@uri&lt;/span&gt;)',
                array(
                    '%filename' =&gt; $filename,
                    '@uri' =&gt; $filename,
                    '!url' =&gt; l(t('this URL'), $url),
                )
            )
        );
    }
    else {
        drupal_set_message(t('Failed to save the file'), 'error');
    }
}
Drupal forms which include a file upload field must also set the encoding type to multipart/form-data.

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


Reference: Drupal.org: Uploading Files



There is a good checklist for debugging your situation here:

https://stackoverflow.com/questions/3586919/why-would-files-be-empty-when-uploading-files-to-php

Not mentioned in the checklist there is that Suhosin, a commonly installed extension, can empty some submitted values. As well as following the checklist in the linked question, check to see if you have Suhosin installed - if so then you may be 

One thing I'd try straight away is whether uploading a very small file triggers this bug. If so that might suggest that you're hitting a limit in PHP's max post data or Suhosin.