Reputation: 43
Please help me reading an excel file upload attached. When I attach an XLS format, my PHPExcel reader is not working. Do you think where am I missing?
HTML
<form role="form" id="myForm" class="add_customer_form" enctype="multipart/form-data">
<label for="fileUpload">Upload File *</label>
<input type="file" id="files" name="file[]" class="form-control"
accept=".csv, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, text/plain, application/vnd.ms-excel"
multiple required>
</form>
AJAX
//USAGE: $("#form").serializefiles();
(function($) {
$.fn.serializefiles = function() {
var obj = $(this);
/* ADD FILE TO PARAM AJAX */
var formData = new FormData();
$.each($(obj).find("input[type='file']"), function(i, tag) {
$.each($(tag)[0].files, function(i, file) {
formData.append(tag.name, file);
});
});
var params = $(obj).serializeArray();
$.each(params, function (i, val) {
formData.append(val.name, val.value);
});
return formData;
};
})(jQuery);
$.ajax({
url: base_url+'customer/customer_add',
type: 'POST',
data: values,
cache: false,
contentType: false,
processData: false,
success: function(data){
console.log(data);
}
});
PHP
foreach($_FILES["file"]['name'] as $file => $fname) {
$file_path = $_FILES["file"]["tmp_name"][$file];
$extension = pathinfo($fname);
if($extension['extension'] == 'xls') {
$objReader = PHPExcel_IOFactory::createReader('Excel5');
}
else {
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
}
$inputFileType = PHPExcel_IOFactory::identify($file_path);
$objPHPExcel = $objReader->load($file_path);
$cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();
print_r($cell_collection); die();
}
DISPLAY ERROR
Upvotes: 1
Views: 3143
Reputation: 212402
Don't trust the file extension.... that xls
file is not a native Excel BIFF-format file, no matter what extension it might claim. It's quite common to find .xls
used as an extension for csv files, or files containing html markup.
Use the IO Factory's identify()
method to tell you what reader to use; or just simply use the IO Factory load()
method to select the correct reader and load the file for you.
Upvotes: 1