How to upload excel file to php server Drupal 7

178

How to upload excel file to php server Drupal 7:

Step 1-  create Menu callback.


/**
* Implements hook_menu().
*/
function my_module_menu() {
$items = array();

$items['site-admin/upload-xls'] = array(
'title' => '',
'page callback' => 'drupal_get_form',
'page arguments' => array('upload_xls_form'),
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}

 

Step 2- Create Form :


/**
* upload_xls_form
* @return string
* @author Rizwan Siddiquee
*/
function upload_xls_form() {
$form['upload_file'] = array(
'#title' => 'Select a file <span class="form-required" title="This field is required.">*</span>',
'#type' => 'file',
'#size' => 29,

);
$form['upload'] = array(
'#type' => 'submit',
'#value' => 'Upload File',
);
return $form;
}

Step 2-  Form Validation:


/**
* Validate upload_xls_form
* @param array $form
* @param array $form_state
*/
function upload_xls_form_validate($form, &$form_state) {
$upload_file_name = $_FILES['files']['name']['upload_file'];
$extension = getFileExtension($upload_file_name);
$extension_array = array('xls', 'xlsx', 'csv', 'XLS', 'XLSX', 'CSV');
if (($_FILES["files"]["type"]["upload_file"] != "text/csv" && $_FILES["files"]["type"]["upload_file"] != 'application/vnd.ms-excel') && !in_array($extension, $extension_array)) {
form_set_error('upload_file', 'Uploaded file is not csv or xls file');
}
}

Step 3-  Form Submit:


/**
* Submit hook for upload_xls_form
* @param array $form
* @param array $form_state
*/
function upload_xls_form_submit($form, &$form_state) {
error_reporting(1);
global $base_path;
$upload_type = $form_state['values']['upload_file_type'];
set_time_limit(3000000000);
$upload_file_name = $_FILES['files']['name']['upload_file'];
$aa = explode('.', $_FILES['files']['name']['upload_file']);
$extension = $aa[count($aa) - 1];
$extension_array = array('xls', 'xlsx', 'csv', 'XLS', 'XLSX', 'CSV');
if (isset($_FILES["files"]["tmp_name"]["upload_file"]) && $_FILES["files"]["tmp_name"]["upload_file"] != '') {
if ($_FILES["files"]["type"]["upload_file"] == "application/vnd.ms-excel" || $_FILES["files"]["type"]["upload_file"] == "text/csv" || in_array($extension, $extension_array)) {
$upload_file_name = $_FILES['files']['name']['upload_file'];
$aa = explode('.', $_FILES['files']['name']['upload_file']);
$extension = $aa[count($aa) - 1];
$filename = time() . '.' . $extension;
$files_dir = 'sites/default/files';
$source = $_FILES['files']['tmp_name']['upload_file'];
$destination = $files_dir . '/upload/' . $filename;
if (!is_dir($files_dir . '/upload/')) {
mkdir($files_dir . '/upload', 0777);
}
move_uploaded_file($source, $destination);
chmod($destination, 0777);
db_truncate('webinar_data')->execute();

<strong>// Add Reader.php (library)</strong>
include drupal_get_path('module', 'admin') . '/reader.php';
$excel = new Spreadsheet_Excel_Reader();
$excel->read($files_dir . '/upload/' . $filename);
foreach ($excel->sheets[0]['cells'] as $key => $val) {
if ($key == 1) {
continue;
}
else {
$nid = isset($val[1]) ? $val[1] : 0;
$cgpa = isset($val[2]) ? $val[2] : 0;
$valid_date = isset($val[3]) ? $val[3] : 0;

// Insert.
db_insert('college_data')
->fields(array(
'nid' => $nid,
'cgpa' => $cgpa,
'valid_upto' => $valid_date,
))->execute();

}
}
drupal_set_message("Uploaded successfully");
}
}
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here