File: /var/www/html/breadsecret.com/upload_awb_excel.php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
//include wp libriries and set timezone to HK
require_once('wp-load.php');
require_once('PHPExcel.php');
date_default_timezone_set("Asia/Hong_Kong");
global $wpdb;
$path = "/var/www/html/ossfs/amber/awb_excel/";
if ($_FILES["excelFile"]["name"] <> ""){
$filename=time()."_".$_FILES["excelFile"]["name"];
$filepath = $path.$filename;
if(move_uploaded_file($_FILES["excelFile"]["tmp_name"], $path.$filename)){
$arr_result['condition'] = 'success';
$arr_result['message'] = 'upload success';
if(file_exists($filepath)){
$arr_data = array(array());
$xlsObj = PHPExcel_IOFactory::load($filepath);
$wsObj = $xlsObj->getActiveSheet();
$maxColIdx = $wsObj->getHighestColumn();
$maxColNo = PHPExcel_Cell::columnIndexFromString($maxColIdx);
$maxCol = $maxColNo-1;
foreach($wsObj->getRowIterator() as $row) {
$xlsRow = $row->getRowIndex();
if(trim($wsObj->getCellByColumnAndRow(0, $xlsRow)->getFormattedValue())!=""){
for($col=0;$col<=$maxCol;$col++){
$arr_data[$xlsRow][] = trim($wsObj->getCellByColumnAndRow($col, $xlsRow)->getFormattedValue());
}
}
}
array_shift($arr_data); // remove first empty row
if(count($arr_data)>0){
for($dataRow=1; $dataRow<count($arr_data); $dataRow++){
$referenceno = $arr_data[$dataRow][1];
$paymentmethod = $arr_data[$dataRow][4];
$transactionreferenceno = $arr_data[$dataRow][6];
$arr_orderID = explode(" ",$referenceno);
$orderID = trim(preg_replace('/[^0-9]/', '', $arr_orderID[0]));
update_post_meta($orderID, 'Payment', $paymentmethod);
update_post_meta($orderID, 'transactionreferenceno', $transactionreferenceno);
}
$arr_result['condition'] = 'success';
$arr_result['message'] = 'import success';
}
} else {
$arr_result['condition'] = 'fail';
$arr_result['message'] = 'uploaded file not found';
}
} else {
$arr_result['condition'] = 'fail';
$arr_result['message'] = 'unable to upload file';
}
}
echo json_encode($arr_result);
?>