I have a form where user upload a excel file when click on upload submit button then file is move into folder but its cannot read records of each rows and columns in excel files. Here is form code
<form id="TypeValidation" method="" enctype="multipart/form-data">
<div>
<input type="file" name="donor_attachment" id="donor_attachment" />
<button type="submit" name="donor_file" id="donor_file" value="donor_file" class="btn btn-success btn-round fileinput-exists">Upload</button>
</div>
</form>
file is submit through ajax
$("#TypeValidation").on('submit',(function(e)
{
var fileUploadID = $(this).find('button[name="donor_file"]').val();
e.preventDefault();
$.ajax({
url: "fn_dsr_wizard_submit.php?submitid="+fileUploadID,
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
dataType: "html",
success: function (result) {
alert(result);
location.reload();
if(result=='1'){
location.replace("donors_dashboard.php");
}else {
location.replace("donors_dashboard.php");
}
}
});
}
));
here is fn_dsr_wizard_sunbmit.php code
if(isset($_REQUEST['submitid']) && $_REQUEST['submitid']=='donor_file'){
global $con;
$userid = $_SESSION['Cont_ID'];
$company_id = $_SESSION['Company_ID'];
//Donor File Attachment
$F_filePath="../../uploads/ds_information/donor/";
$_FILES['donor_attachment']['name'];
if ($_FILES['donor_attachment']['name']!=""){
$donor_attachment = date('dmyhis')."_".$userid."_".$company_id."_".$_FILES['donor_attachment']['name'];
move_uploaded_file($_FILES['donor_attachment']['tmp_name'], $F_filePath.$donor_attachment);
$fileextension = explode(".",$donor_attachment);
if($fileextension[1]=="xls") {
$data = new Spreadsheet_Excel_Reader();
$html="<table border='1'>";
for($i=0;$i<count($data->sheets);$i++) // Loop to get all sheets in a file.
{
if(count($data->sheets[$i][cells])>0) // checking sheet not empty
{
for($j=1;$j<=count($data->sheets[$i][cells]);$j++) // loop used to get each row of the sheet
{
$html.="<tr>";
for($k=1;$k<=count($data->sheets[$i][cells][$j]);$k++) // This loop is created to get data in a table format.
{
$html.="<td>";
$html.=$data->sheets[$i][cells][$j][$k];
$html.="</td>";
}
$eid = mysqli_real_escape_string($con,$data->sheets[$i][cells][$j][1]);
$name = mysqli_real_escape_string($con,$data->sheets[$i][cells][$j][2]);
$email = mysqli_real_escape_string($con,$data->sheets[$i][cells][$j][3]);
$dob = mysqli_real_escape_string($con,$data->sheets[$i][cells][$j][4]);
//$query = "insert into excel(eid,name,email,dob) values('".$eid."','".$name."','".$email."','".$dob."')";
//mysqli_query($connection,$query);
$html.="</tr>";
}
}
}
$html.="</table>";
echo $eid;
echo $name;
echo $email;
echo $dob;
}
}
else {$donor_attachment=$_SESSION['donor_attachment']; unset($_SESSION['donor_attachment']);}
if($_FILES['donor_attachment']['name'] !=''){
$_SESSION['fileInsert']="donor_file_success_msg";
echo 1;
}
else{
$_SESSION['filenotInsert']="donor_file_error_msg";
echo 0;
}
//}
}
Issue is that recods are not read from each columns and rows in excel and when add this line $excel->read('$donor_attachment');
after this line $data = new Spreadsheet_Excel_Reader();
then show me alert error of file is not readable. I want that records are read from excel file
After I modified my code then read a records from excel file
if(isset($_REQUEST['submitid']) && $_REQUEST['submitid']=='donor_file'){
global $con;
$userid = $_SESSION['Cont_ID'];
$company_id = $_SESSION['Company_ID'];
//Donor File Attachment
$F_filePath="../../uploads/ds_information/donor/";
$_FILES['donor_attachment']['name'];
if ($_FILES['donor_attachment']['name']!=""){
$donor_attachment = date('dmyhis')."_".$userid."_".$company_id."_".$_FILES['donor_attachment']['name'];
move_uploaded_file($_FILES['donor_attachment']['tmp_name'], $F_filePath.$donor_attachment);
$filepath = "../../uploads/ds_information/donor/".$donor_attachment;
$fileextension = explode(".",$donor_attachment);
if($fileextension[1]=="xls") {
$data = new Spreadsheet_Excel_Reader();
$data->read($filepath);
$html="<table border='1'>";
for($i=0;$i<count($data->sheets);$i++) // Loop to get all sheets in a file.
{
if(count($data->sheets[$i][cells])>0) // checking sheet not empty
{
for($j=3;$j<=count($data->sheets[$i][cells]);$j++) // loop used to get each row of the sheet
{
$html.="<tr>";
for($k=3;$k<=count($data->sheets[$i][cells][$j]);$k++) // This loop is created to get data in a table format.
{
$html.="<td>";
$html.=$data->sheets[$i][cells][$j][$k];
$html.="</td>";
}
$eid = mysqli_real_escape_string($con,$data->sheets[$i][cells][$j][1]);
$name = mysqli_real_escape_string($con,$data->sheets[$i][cells][$j][2]);
$email = mysqli_real_escape_string($con,$data->sheets[$i][cells][$j][3]);
$dob = mysqli_real_escape_string($con,$data->sheets[$i][cells][$j][4]);
//$query = "insert into excel(eid,name,email,dob) values('".$eid."','".$name."','".$email."','".$dob."')";
//mysqli_query($connection,$query);
$html.="</tr>";
}
}
}
$html.="</table>";
echo $html;
}
}
else {$donor_attachment=$_SESSION['donor_attachment']; unset($_SESSION['donor_attachment']);}
if($_FILES['donor_attachment']['name'] !=''){
$_SESSION['fileInsert']="donor_file_success_msg";
echo 1;
}
else{
$_SESSION['filenotInsert']="donor_file_error_msg";
echo 0;
}
//}
}