Image Upload in a Custom Module I'm writing a custom module, and I need it to upload an image. I'm having trouble finding good documentation on this, but I think I'm close.

What am I missing? $file returns false in the form submission.

function mymodule_custom_content_block_form($form_state){
    $form = array();
    $form['custom_content_block_text'] = array(
        '#type' =&gt; 'textarea',
        '#title' =&gt; t('Block text'),
        '#default_value' =&gt; variable_get('mymodule_custom_content_block_text'),
        '#required' =&gt; true,
    );
    $form['custom_content_block_image'] = array(
        '#type' =&gt; 'file',
        '#name' =&gt; 'custom_content_block_image',
        '#title' =&gt; t('Block image'),
        '#size' =&gt; 40,
        '#description' =&gt; t("Image should be less than 400 pixels wide and in JPG format."),
    );  
    $form['submit'] = array(
        '#type' =&gt; 'submit',
        '#value' =&gt; t('Update'),
    );
    return $form;
}

function mymodule_custom_content_block_form_submit($form, &amp;$form_state){
    if(isset($form_state['values']['custom_content_block_image'])){
        $validators = array('file_validate_extensions' =&gt; array('jpg jpeg'));
        $file = file_save_upload('custom_content_block_image', $validators, 'public://');
        if($file == false){
            drupal_set_message(t("Error saving image."), $type = "error", $repeat = false);
        }
        else{
            $file-&gt;status = FILE_STATUS_PERMANENT;
            $file = file_save($file);   
        }
    }
    variable_set('mymodule_custom_content_block_text', $form_state['values']['custom_content_block_text']);
    drupal_set_message(t('Custom Content Block has been updated.'));
}
If you don't mind my saying you're doing this the hard way. Drupal has a managed_file element type that handles most of this logic for you:

function mymodule_custom_content_block_form($form, &amp;$form_state) {
  $form['custom_content_block_image'] = array(
    '#type' =&gt; 'managed_file',
    '#name' =&gt; 'custom_content_block_image',
    '#title' =&gt; t('Block image'),
    '#size' =&gt; 40,
    '#description' =&gt; t("Image should be less than 400 pixels wide and in JPG format."),
    '#upload_location' =&gt; 'public://'
  ); 

  return $form; 
}

function mymodule_custom_content_block_form_submit($form, &amp;$form_state) {
  if (isset($form_state['values']['custom_content_block_image'])) {
    $file = file_load($form_state['values']['custom_content_block_image']);

    $file-&gt;status = FILE_STATUS_PERMANENT;

    file_save($file);
  }
}