function test_import($form, &$form_state) {
$form['csv_file'] = array(
'#title' => t('Upload a csv file'),
'#type' => 'file',
'#description' => t('Must be smaller than 5MB in size.'),
);
$form[] = array(
'#type' => 'submit',
'#value' => 'Process',
);
return $form;
}
function test_import_validate($form, &$form_state) {
$validators = array(
'file_validate_extensions' => array( 'csv' ),
'file_validate_size' => array(variable_get('btn_file_size', '5') * 1024),
);
// the others validators are http://api.drupal.org/api/drupal/includes!file.inc/group/file/7
$destination = FALSE;
// $destination = 'public://testimport/';
$fileObj = file_save_upload('csv_file', $validators, $destination);
if (!$fileObj) {
form_set_error('csv_file', t('Oh Unable to access file or file is missing.'));
}
}
function test_import_submit($form, &$form_state) {
$fileObj = file_save_upload( 'csv_file');
if (!$fileObj) {
form_set_error('csv_file', t('Unable to access file or file is missing.'));
}
ini_set('auto_detect_line_endings', true);
$file = fopen( drupal_realpath($fileObj->uri), 'r');
while(!feof($file)) {
$line = fgets($file);
$data = explode("\t", $line);
dpm($data[0]);
}
fclose($file);
}
related :
http://api.drupal.org/api/drupal/developer%21topics%21forms_api_reference.html/7#managed_file
http://api.drupal.org/api/examples/image_example%21image_example.pages.inc/7
Thanks very much for this. It was more than helpful.
ReplyDeleteI had a bit of trouble at the function test_import_submit node, but it turned out I misspelled some words that's what was messing up my code.
Diana Guess, Toronto IT tech support