i have an simple xls file and it's print me that :
PK!b�h^�[Content_Types].xml �(����N�0E�H�C�-Jܲ#5��Q>�ēƪc[�ii����B�j7���{2��h�nm���ƻR����U^7/���%��rZY�#1__�f��q��R4D�AJ�h>����V�ƹ�Z�9����NV�8ʩ����ji){^��-I�"{�v^�P!XS)bR�r��K�s(�3�c�0��������7M4�����ZƐk+�|\|z�(���P��6h_-[�#�!���Pk���2n�}�?�L��� ��%���d����dN"m,�ǞDO97*�~��ɸ8�O�c|n���E������B��!$}�����;{���[����2���PK!�U0#�L_rels/.rels �(���MO�0��H�����ݐBKwAH�!T~�I����$ݿ'T�G�~����<���!��4��;#�w����qu*&r�Fq���v�����GJy(v��*����K��#F��D��.W ��=��Z�MY�b���BS�����7��ϛז�� ?�9L�ҙ�sbgٮ|�l!��USh9i�b�r:"y_dl��D���|-N��R"4�2�G�%��Z�4�˝y�7 ë��ɂ�����PK!��3_I�xl/workbook.xml�Umo�8�~���w�M0oj� o�J{����~�tr�+�9c�T���CH�����^���c?3����ӡm�&.���ϐi��%������ LcP�+i#:�6�~�����^�ݝ;�am�J��mE�Z:���u`��l������^2Z5c�ml!�n)��!���U���blY�f����P�~X���=p-���� ��q��'P�h�����5��� ���#��&0�������#�3�7�cdc���1x�kK��uO���AV� �{���0Hk�J�� 9qs��y�v3Kנ}�mu��h蠲�+V�M�b�^mȱ�Gހ�qV1��IΗ�(YE�F]��x��� �m���b�������jns�I-#���{�Aa���Wiѻᒪ�e�6�����߂��MٰS��}�K����2i�c�8�����d���RI�/�ϐ�?���^�����Eq� ��J�6��4O��x؊]B�e> �7�BzQ!��c�5��t�L���b�(y�|
my code is : i tried :
$file = file_get_contents('...\TestExcel.xlsx');
echo $file;
and also :
$file = file('......\TestExcel.xlsx')
foreach($file as $files){
echo $files;
}
Whats wrong please ?
Looks like a binary file. An XLSX file is a Microsoft Excel Open XML Format Spreadsheet file. It's a ZIP-compressed, XML-based spreadsheet, which you can't open the way you tried.
I think it is possible to unzip it and get to the actual content of the spreadsheet, which is basically just xml.
Related
Trying to import XLS file to my mysql table.
But returns error below.
excelimport/storage/framework/laravel-excel/laravel-excel-BBA94ICWI0E7wrAuLevn89J6OElWa1qR.xls is not recognised as an OLE file
Here's my code.
I know that, the file got wrong format, but when I save as an excel (without changing anything), It works well.
But problem is; my users will upload files like that.
And I need to solve it without saying "please export your xls file as excel again".
$file = $request->file('file')->store('import');
Excel::import(new UserImportModel, $file);
Is it possible and if is how can i change excel file extension while uploading or before saving file on server? I am using php and mysql.
Thankyou
You can do something like this.
move_uploaded_file($_FILES['file']['tmp_name'], upload_PATH.'/'.$_FILES['file']['name'].'x');
But that will only change the file name with the xlsx extension. It will not actually convert the file to xlsx format.
As previously mentioned in a different reply, changing the extension won't actually change the format, and it's not a good idea to serve a .xls file as .xlsx, since this will only confuse anyone trying to read it.
What you could do (disregarding potential problems with converting and verification of the file) is read the uploaded file into a library like PHPExcel (http://phpexcel.codeplex.com) and then use the builtin functions to export it as an .xlsx file. Sample below:
// Create a reader to read .xls format
$reader = PHPExcel_IOFactory::createReader('Excel5');
// Read the .xls file from upload storage
$workbook = $reader->load($_FILES['file']['tmp_name']);
// Create a writer to output in .xlsx format
$writer = PHPExcel_IOFactory::createWriter($workbook, 'Excel2007');
// Save file to destination .xlsx path
$writer->save($destination_path);
Keep in mind that although this might work perfectly well, the conversion might mess with the contents of the file. This might not be desirable, as the conversion can cause data loss, formatting changes and all sorts of weirdness.
I'm struggling with my XML to XLS code in PHP.
I have PHP code that exports MySql data to XML and I have code that writes XML to XLS.
This all works fine, except that when I open the xls file, I get an error:
"The file you are trying to open is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?"
my code:
<?php
$file = 'filename';
$url = "$file.xml"; // from xml file
header( "Content-Type: application/vnd.ms-excel" );
header( "Content-disposition: attachment; filename=$file.xls" ); // to xls file
if (file_exists($url)) {
$xml = simplexml_load_file($url);
echo 'Voornaam'."\t" . 'Achternaam'."\t" . 'Geslacht'."\t" . 'Instrument'."\t\n";
foreach($xml->lid as $lid)
{
echo $lid->voornaam."\t" . $lid->achternaam."\t" . $lid->geslacht."\t" . $lid->instrument."\n";
}
}
?>
I found that it has something to do with the MIME_Types https://filext.com/faq/office_mime_types.php
The header("Content-Type: application/vnd.ms-excel"); is for the older versions of excel. But when I use the xlsx type, the file won't open at all.
Like I said, this code runs fine, downloads the Excel file in xls format to my computer. But how can I open it without the error?
I would also like to upgrade the file to xlsx format if someonw knows how to open the file in that format.
Well, your header and filename are both saying "this is an XLS file" - but it isn't, it's actually a TSV file (Tab-Separated Values). So yes, the message is accurate: the file extension is misleading.
To write an actual, valid XLS (or XLSX), it is easiest to use a library which does this for you - both these formats are complex and tricky. I've had best results using PhpExcel (example code).
When Excel opens an .xls file, it expects it to be in normal old binary Excel format. You seem to be outputting tab separated text (.tsv format), which Excel understands but doesn't want to open without issuing a warning. I think this warning was introduced with Excel 2007, and that earlier versions opened happily without warnings. Unfortunately I have no sources handy for this info, only personal experience.
I want to read the Uploaded Excel file in php. so i downloaded excel_reader2.php
from following link
link
when I inserted this into my code i got following Error:
The filename try.xlsx is not readable.
My code is:
if (file_exists($filepath))
{
echo "File present";
}
else
{
die('The file ' . $filename . ' was not found');
}
$data = new Spreadsheet_Excel_Reader($filename,false);
$data->read($filename);
$data->val(1, 'A');
echo $data;
So after searching in Google I got link that is Here
After Following this also i am getting same error.
So can any one help me, where I am going wrong?
Thank you.
PEAR SEW cannot read OfficeOpenXML (.xlsx) format files, only the older BIFF (.xls) format files. If you want to read .xlsx files, then you need a reader library that does support that format such as PHPExcel
I'm trying to let users upload files onto my website, but unfortunately some of them seem to turn corrupt when reading them. I've tried both images and html files, and all the images come through corrupt (the HTML files come through fine).
To upload the files I'm using a standard HTML form and the PHP $_FILES array. I'm then using the following code to read the contents of the file:
$filename = $_FILES['varname']['tmp_name'];
$handle = fopen($filename, "r");`
$contents = fread($handle, filesize($filename));
fclose($handle);
Unfortunately the value of $contents is now slightly different to the file I uploaded (here's a snippet from the top of the file):
Original file:
ˇÿˇ·ExifII*ˇÏDucky<ˇÓAdobed¿ˇ€Ñ
New file:
ˇÿˇ· Exif II* ˇÏ Ducky < ˇÓ Adobe d¿ ˇ€ Ñ
As you can see there's a difference in the spacing. Any ideas what would be causing this? Am I handling the file read incorrectly for binary files? It seems odd that it's fine for any text files I upload..
Thanks!
I usually output files like this:
header("Content-Disposition: attachment; filename=\"$fileName\"");
readfile("$HOME_DIR/uploads/$fileName");
exit();
Anyway, to try to debug your problem, you should first understand which phase is failing. Upload or download? To check, just go to your webserver and download the file via FTP, then open it in a binary editor. If it is already corrupt then you need to investigate your upload phase, otherwise it's the other way around.
how do you print $contents ? Are you sure this is a problem with reading the file ?
I guess that maybe this is a problem with PRINTING the file to the output... Try printing it binary way. Something like:
$data = unpack("C*", $contents);
foreach ($data as $v)
{
echo $v, ' ';
}
and compare that with binary dump of the original file...