I'm developing a module that uploads CSV file.
/**
* Implements hook_menu().
*/
function geo_points_menu(){
$items['geo-points-upload'] = array(
'title' => 'Geo points',
'description' => 'Upload users',
'page callback' => 'drupal_get_form',
'page arguments' => array('geo_points_form'),
'access arguments' => array('access content'),
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
'menu_name' => 'main-menu',
);
return $items;
}
function geo_points_form($form, &$form_state) {
$form = array();
// Use the #managed_file FAPI element to upload a file.
$form['file_fid'] = array(
'#title' => t('File'),
'#type' => 'managed_file',
'#description' => t('The uploaded file will be processed.'),
'#default_value' => variable_get('file_fid', ''),
'#upload_validators' => array(
'file_validate_extensions' => array('csv'),
),
'#upload_location' => 'public://csv_uploads',
'#process' => array('geo_points_my_file_element_process')
);
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => 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, &$form_state, $form) {
$element = file_managed_file_process($element, $form_state, $form);
$element['upload_button']['#access'] = FALSE;
return $element;
}
function geo_points_form_submit(&$form, &$form_state) {
// Load the file.
$file = file_load($form_state['values']['file_fid']);
dpm(csv_to_array($file->uri));
file_usage_delete($file, 'geo_points');
// // Change status to permanent.
// $file->status = FILE_STATUS_PERMANENT;
// // Save.
// $uploaded = file_save($file);
// if ($uploaded == TRUE) {
// dpm(csv_to_array($file->uri));
// }
// else {
// drupal_set_message(t('The file could not be uploaded. Please contact the site administrator.'), 'error');
// }
//$file_path = $file->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] => 1 [:db_insert_placeholder_1] => points2014-01-16T14-22-48.csv [:db_insert_placeholder_2] => public://csv_uploads/points2014-01-16T14-22-48.csv [:db_insert_placeholder_3] => text/csv [:db_insert_placeholder_4] => 84 [:db_insert_placeholder_5] => 0 [:db_insert_placeholder_6] => 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?