I have this code which save xml api as an xlsx. But When I save it, i can not open on computer. But txt doc and other formats support it. How can I save it as an xlsx file successfully.
<?php
$xlsx = file_get_contents("http://cbu.uz/uzc/arkhiv-kursov-valyut/xml/");
file_put_contents("currency/kurs.xlsx", $xlsx);
?>
Related
I am trying to read contents of zipped file as
$subda=file_get_contents('http://www.yifysubtitles.com/subtitle/thewilbyconspiracy1975dvdripxvid-english-128250.zip');
And trying to upload at my online server as below
$this->load->library('zip');
$data = $subda;
$name = 'myfile.srt';
$this->zip->add_data($name, $data);
$this->zip->archive('assets/subtitles/myzipfile.zip');
But when I check this uploaded file at my server it does not compressed properly.
it does not contain any data.
when I echo $subda it give results like.
Where I am wrong...
through file_get_contents I am already getting contents of zip file.
You cannot do:
$subda=file_get_contents('http://www.yifysubtitles.com/subtitle/thewilbyconspiracy1975dvdripxvid-english-128250.zip');
This will just load the ZIP content into the string rather than uncompressed.
See this on how you can read using a lib in php:
Best way to read zip file in PHP
I am uploading the excel file in amazon s3 server and its getting uploaded successfully, but while reading that same excel file in php getting the below data.
����E� �ɂ^R,5��� I
�ҧ���{ߙw�Ig�FS���셄�0b����[�Jb���k�>+��s��Q�z�� ,E�� º
����u��� ����O�DC�pn��Wi�_��p1F�h��axu���\tWW�N�b��~�:� >����*���p�1�ڧ 掓�f��Q����PK)��e�PK�;:K
xl/styles.xml�Y[o�0}߯���R�e
�Z�L���j�4i�&�8�Uǎ�B�| ! ��jT>���%7�l�Q��EA8��A�B���(�xA!�������rF�m�����J�r�"Lq���c�jb.2$UQ$N���B7ʨ�v:GN����&Y���|¤�QA��F
<�A`��<RR�\^]ޜ���9%��Ŝ-x���W<�DIW���rD2� 蘟�ʰ
xl/sharedStrings.xmlPK�;:K)��e��xl/workbook.xmlPK�;:K�,q�^�
Gxl/styles.xmlPK�;:K�!49���docProps/app.xmlPK�;:K�ma�m�docProps/core.xmlPK�;:K�������docProps/custom.xmlPK�;:K�#��C��[Content_Types].xmlPKh
using the below php code to print the data
$s3Client->registerStreamWrapper();
$stream = file_get_contents(utf8_encode($this->stock_catalog_xls_path), 'r');
echo $stream;
so please can you give me any idea how to read this excel data in php.
You are reading Excel as a raw file.
You need to pass the contents to an Excel library, so you can read them with cells, sheets format.
Below is the library that can help you in converting the raw file to excel readable format.
https://github.com/PHPOffice/PHPExcel
Sample implementation on how to read Excel file.
https://github.com/PHPOffice/PHPExcel/blob/1.8/Examples/07reader.php
Hope it helps.
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 created xml file in php. This file is successfully saved in my server directory.
Now, I want to download this file only through php, not javascript.
How can I save this file?
Try file_get_contents() and file_put_contents().
<?PHP
$xml = file_get_contents("http://yoursite.com/yourxml.xml"); // your file is in the string "$xml" now.
file_put_contents("/path/to/file/yourxml.xml", $xml); // now your xml file is saved.
?>