Skip to content

Instantly share code, notes, and snippets.

@ThienTranDuy
Last active October 6, 2023 03:14
Show Gist options
  • Save ThienTranDuy/391f936e791254847b931a7876962524 to your computer and use it in GitHub Desktop.
Save ThienTranDuy/391f936e791254847b931a7876962524 to your computer and use it in GitHub Desktop.
PHP proccess excel upload file
$fileName = $file['name'];
$fileUploadTmp = $file['tmp_name'];
$fileType = $file['type'];
$fileError = $file['error'];
$fileContent = file_get_contents($file['tmp_name']);
$extension = strtoupper(pathinfo($fileName, PATHINFO_EXTENSION));
$allowFiles = ['XLSX', 'CSV'];
// check upload is ok
if ($fileError) {
throw new Exception($fileError);
}
// check type
if (!in_array($extension, $allowFiles, true)) {
throw new Exception(__("The upload file is not valid (.xlsx, .csv)", 'ode-chart'));
}
// read file
require_once ODE_CHART_PATH . '/library/PHPExcel/Classes/PHPExcel.php';
$inputFileType = PHPExcel_IOFactory::identify($fileUploadTmp);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($fileUploadTmp);
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
//Loop through each row of the worksheet in turn
echo "<pre>";
for ($row = 1; $row <= $highestRow; $row++) {
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
print_r($rowData);
//Insert into database
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment