Read vbaProject.bin for macrosCode in PHP - php

I am trying to create a .xlsm excel file while reading the macrosCode from the vbaProject.bin file.
I implemented the following snippet:
$filename = 'private://zips/xl/vbaProject.bin';
$handle = fopen($filename, "rb");
$contents = stream_get_contents($handle);
fclose($handle);
$spreadsheet->setMacrosCode($contents);
This is a workaround because the PhpSpreadsheet library does not support generating excel file with macros. But somehow, when reading the vbaProject.bin file, the format seems incorrect, might be because of the encoding. https://i.stack.imgur.com/7SGbK.png
My question is: could I read the vbaProject.bin somehow with the proper format?

Related

Gz giving unreadable data

I am creating sitemap.xml using php. In which i have to create gz file which contains number of links.
Using following code.
$gzfile = $xmlfile.".gz";
$fp = gzopen ($gzfile, 'w9');
gzwrite ($fp, $photoxmldata);
gzclose($fp);
But when i open created gz file, it gives unreadable data.
Please help.

PHP get raw data of PDF to POST using CURL

I have been tryign to get the raw contents of a PDF file using PHP to POST to an API using CURL. I have tried using file_get_contents (though the resulting file is not readable) and I have tried
$filename = "/files/example.pdf";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
Then I post contents and again the file is not readable by a PDF reader
Any idea's?
Thanks

Generate OpenDocument Spreadsheets with PHP

I'm trying to generate OpenDocument Streadsheets using PHP.
This is my code:
$content_xml = '<?xml version="1.0" encoding="UTF-8"?><office:document-content ...';
$file = 'spreadsheet.ods'; // this one is beeing generated. It is not existent yet...
$zipfile = 'container.zip'; // this one already exists. It's in the same directory
// this script is in. I created the container.zip by unziping a usual .ods file,
// removing the content.xml and ziping again
$handle1 = fopen($zipfile, "r");
$handle2 = fopen($file, "w");
// writing the content of the `container.zip` to the `spreadsheet.ods`
fwrite($handle2, fread($handle1, filesize($zipfile)));
fclose($handle1);
fclose($handle2);
$zip = new ZipArchive();
$zip->open($file, ZIPARCHIVE::CREATE);
// adding the xml string to the zip file
$zip->addFromString('content.xml',$content_xml);
$zip->close();
header('Content-disposition: attachment; filename="'.$file.'"');
header('Content-Type: application/vnd.oasis.opendocument.spreadsheet');
header('Content-Length: ' . filesize($file));
readfile($file);
unlink($file);
If I open the generated file with OpenOffice - everything looks fine. But MS Excel for some reason is not able to open the file. I think the XML string in $content_xml is corrupt. I don't want to bother anybody with that string - it is huge! I got it from a simple ods spreadsheet. I just edited the <office:body>...</office:body> part.
I wondering whether this is a good way to generate spreadsheets. Does anybody know a tutorial for this task:
do all that zip stuff
structure of a simple content.xml in an ods file
Regarding "1. do all that zip stuff":
what I do here is generating the content.xml and adding it to the root of the .ods structure
Configurations2 // Folder
META-INF // Folder
Thumbnails // Folder
meta.xml
mimetype
settings.xml
styles.xml
By doing
$zip->addFromString('content.xml',$content_xml);
But how can I add a file NOT to the root of the structure but (let' say) to Configurations2/images/Bitmamps?
You can find full details of the OpenOffice (OASIS) format on the OASIS technical pages while The Open Office website provides DTDs and some tutorials

write the data to saved excel without affecting the format

I have saved excel sheet on my server,it has an format i trying to write data to the saved excel but when i trying write its removing the excel format (i.e it removing header color everything) it write start data in the first cell,i want to write the data to saved excel without changeling the excel format start from cell 10, below is the image how my excel looks like before performing the function.
and after how its,looks like.
before:
after
mycode
<?php
$File = "Book1.xls";
$Handle = fopen($File, 'w');
$Data = "Abdul Rahim\n";
fwrite($Handle, $Data);
$Data = "Raja\n";
fwrite($Handle, $Data);
print "Data Written";
fclose($Handle);
?>
can anyone help me how to do this .
You cannot modify the XLS just with fwrite. It has a strict format.
You could switch to CSV or use a third-party library like PhpExcel.

PHP corrupt file upload

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...

Categories