PDO - Duplicate entry on file upload I'm developing a module that uploads CSV file.

/**
 * Implements hook_menu().
 */
function geo_points_menu(){
    $items['geo-points-upload'] = array(
        'title' =&gt; 'Geo points',
        'description' =&gt; 'Upload users',
        'page callback' =&gt; 'drupal_get_form',
        'page arguments' =&gt; array('geo_points_form'),
        'access arguments' =&gt; array('access content'),
        'access callback' =&gt; TRUE,
        'type' =&gt; MENU_NORMAL_ITEM,
        'menu_name' =&gt; 'main-menu',
    );

    return $items;
}

function geo_points_form($form, &amp;$form_state) {
    $form = array();

    // Use the #managed_file FAPI element to upload a file.
    $form['file_fid'] = array(
        '#title' =&gt; t('File'),
        '#type' =&gt; 'managed_file',
        '#description' =&gt; t('The uploaded file will be processed.'),
        '#default_value' =&gt; variable_get('file_fid', ''),
        '#upload_validators' =&gt; array(
            'file_validate_extensions' =&gt; array('csv'),
        ),
        '#upload_location' =&gt; 'public://csv_uploads',
        '#process' =&gt; array('geo_points_my_file_element_process')
    );

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

    return $form;
}

/* It disables the default upload button that comes with this #managed_file form */
function geo_points_my_file_element_process($element, &amp;$form_state, $form) {
    $element = file_managed_file_process($element, $form_state, $form);
    $element['upload_button']['#access'] = FALSE;

    return $element;
}

function geo_points_form_submit(&amp;$form, &amp;$form_state) {
    // Load the file.
    $file = file_load($form_state['values']['file_fid']);
    dpm(csv_to_array($file-&gt;uri));
    file_usage_delete($file, 'geo_points');
//    // Change status to permanent.
//    $file-&gt;status = FILE_STATUS_PERMANENT;
//    // Save.
//    $uploaded = file_save($file);
//    if ($uploaded == TRUE) {
//        dpm(csv_to_array($file-&gt;uri));
//    }
//    else {
//        drupal_set_message(t('The file could not be uploaded. Please contact the site administrator.'), 'error');
//    }

    //$file_path = $file-&gt;uri;

    //Function call to process file contents.
    //csv_import_create_node($file_path);

}

/**
 * @param string $filename
 * @param string $delimiter
 * @return array|bool
 */
function csv_to_array($filename='', $delimiter=',')
{
    if(!file_exists($filename) || !is_readable($filename))
        return FALSE;

    $header = NULL;
    $data = array();
    if (($handle = fopen($filename, 'r')) !== FALSE)
    {
        while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE)
        {
            if(!$header)
                $header = $row;
            else
                $data[] = array_combine($header, $row);
        }
        fclose($handle);
    }
    return $data;
}


When I upload the same file more than one time I'm getting the following exception:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'public://csv_uploads/points2014-01-16T14-22-48.csv' for key 'uri': INSERT INTO {file_managed} (uid, filename, uri, filemime, filesize, status, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6); Array ( [:db_insert_placeholder_0] =&gt; 1 [:db_insert_placeholder_1] =&gt; points2014-01-16T14-22-48.csv [:db_insert_placeholder_2] =&gt; public://csv_uploads/points2014-01-16T14-22-48.csv [:db_insert_placeholder_3] =&gt; text/csv [:db_insert_placeholder_4] =&gt; 84 [:db_insert_placeholder_5] =&gt; 0 [:db_insert_placeholder_6] =&gt; 1390315536 ) in drupal_write_record() (line 7170 of C:\wamp\www\drupal7_poc2\includes\common.inc).


I have 2 questions:
1. How avoid this exception.
2. After I upload the file and read it's content I don't the file anymore, can I read the stream only without saving the file?
Use a try...catch block and recover in whatever way makes most sense to your application. 
The file is already on the disk by the time you get access to it (in the /tmp folder), so no. If you don't explicitly choose to keep it, it will be deleted either by a system job or the system module's cron run.