I need to make a PHP function that will count the number of lines in a Word File (.doc, docx)
This code does not seem to work correctly for me :
$name = 'test.doc';
$line_count = count(file($name));
echo $line_count
My guess is that this script does not work well with .doc or .docx but works good in .txt files. Are there any alternatives available out there? Tnx!
Only .docx can be opened with simple PHP as it is a zip file:
$zip = new ZipArchive;
if ($zip->open('test.docx') === TRUE) {
$xml = $zip->getFromName('docProps/app.xml');
$zip->close();
} else {
$xml = false;
}
if($xml){
$xml = simplexml_load_string($xml);
echo $xml->Lines;
}
Related
I'm working on a project where I need to create a .docx document. I was using PHPWord, loading a template and then saving the file. This document has a lot of nested tables and PHPWord is breaking the tables after some replaces in the template.
So I decided to save the document as Word XML document (.xml) and do the replaces myself. I will load the text into a variable, do the replaces and then save as a new word document. My problem is that I don't know how to create a .docx document using a .xml.
Would you have some code snippets I could use?
Thanks for any help
I have come to the piece of code below. It saves the file but when I try to open using word it gives me invalid document
$xmlString = simplexml_load_file($this->config->application->fileTemplateFolder.'coi.xml')->asXML();
$xmlString = str_replace('${coi_number}', $coi['application_number'], $xmlString);
$path = $this->config->application->fileTemplateFolder.'test.docx';
$zip = new ZipArchive();
$zip->open($path, ZipArchive::CREATE);
$zip->addFromString("word/document.xml", $xmlString);
$zip->close();
Here is how I solved the issue:
private function CreateWordDocument($xmlString) {
$templateFolder = $this->config->fileTemplateFolder;
if(!endsWith($templateFolder, '/'))
$templateFolder = $templateFolder.'/';
$temp_file = tempnam(sys_get_temp_dir(), 'coi_').'.docx';
copy($templateFolder. 'coi.docx', $temp_file);
$zip = new ZipArchive();
if($zip->open($temp_file)===TRUE) {
$zip->deleteName('word/document.xml');
$zip->addFromString("word/document.xml", $xmlString);
$zip->close();
return $temp_file;
}
else {
return null;
}
}
I am retrieving my google map in a kmz format like this:
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/temp/map.kmz', file_get_contents('https://mapsengine.google.com/map/kml?mid=zLucZBnh_ipg.kS906psI1W9k') );
$zip = new ZipArchive;
$res = $zip->open($_SERVER['DOCUMENT_ROOT'].'/temp/map.kmz');
if ($res === true)
{
trace("Number of files: $res->numFiles".PHP_EOL);
for( $i = 0; $i < $res->numFiles; $i++ )
{
$stat = $res->statIndex( $i );
print_r( basename( $stat['name'] ) . PHP_EOL );
}
}
But no files are showing and $zip->extractTo() is not working either. The file is downloaded on the server and I can extract it manually though. I have tried renaming the file to .zip or .kmz, still not working. I have opened the map.kmz file in Winrar and it does indeed say that it is a zip file format.
Any idea why it's not working? Do I need some special permissions to read the number of files or extract?
Check your file types .mkz and .kmz.
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/temp/map.mkz',
file_get_contents('https://mapsengine.google.com/map/kml? mid=zLucZBnh_ipg.kS906psI1W9k') );
$zip = new ZipArchive;
$res = $zip->open($_SERVER['DOCUMENT_ROOT'].'/temp/map.kmz');
Got tired of the damn class not working, tried this method instead and it works:
$data = file_get_contents("https://mapsengine.google.com/map/kml?mid=zLucZBnh_ipg.kS906psI1W9k");
file_put_contents($_SERVER['DOCUMENT_ROOT'].'/temp/kmz_temp', $data);
ob_start();
passthru("unzip -p {$_SERVER['DOCUMENT_ROOT']}/temp/kmz_temp");
$xml_data = ob_get_clean();
header("Content-type: text/xml");
echo $xml_data;
exit();
I'm trying to extract a zip file witch contains php files.
Taking for example a zip name 'test.zip' and inside, a zip.php file with this code:
<?php
class php{
}
?>
I've tried with two differents snippets to extract the zipe file:
$zip = zip_open ( 'test.zip' );
while($file = zip_read($zip)){
$archivo = (zip_entry_name($file));
zip_entry_open($zip, $file);
$contenido = zip_entry_read($file);
$fopen = fopen($archivo,'w+');
fwrite($fopen, $contenido);
}
<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip');
if ($res === TRUE) {
$zip->extractTo('./');
$zip->close();
echo 'ok!';
} else {
echo 'ko!';
}
?>
But in both cases, the result is always the same:
<?php
class phpclass{
function clase(){
}
}
?>
The file extracted is twice longer, and has twice of empty breaklines than original.
Is there anyway to extract an identical copy of files contained in the zip file?
Please replace this:
$fopen = fopen($archivo,'w+');
... with this:
$fopen = fopen($archivo,'w+b');
The b flag stands for "binary" and disables line feed conversion. Flags are described in the fopen() manual page.
<?php
$zip = new ZipArchive;
if ($zip->open('test.docx') === TRUE) {
$xmlString = $zip->getFromName('word/document.xml');
$xmlString = str_replace('$FIRST_AND_LAST_NAME', 'John Doe', $xmlString);
$zip->addFromString('word/document.xml', $xmlString);
echo 'ok';
$zip->close();
} else {
echo 'failed';
}
?>
I had using this code to replace some word in my docx file, but how if i want to save it in different file name instead of replace the original file. thanks.
I need to process the contents of a zipped file, but I can't change the permissions on the server where my program will be hosted.
This means that I can't download the zip file to the server, so I need to read the contents of the file into a variable without writing it to the file system.
Can I grab the string contents of such variable and get the unzipped contents into a new variable?
So far, I've looked into using the zip php extension, and the pclzip library, but both need to use actual files.
This is what I want to do in pseudo code:
$contentsOfMyZipFile = ZipFileToString();
$myUnzippedContents = libUnzip($contentsOfMyZipFile);
Any ideas?
Look at this example.
<?php
$open = zip_open($file);
if (is_numeric($open)) {
echo "Zip Open Error #: $open";
} else {
while($zip = zip_read($open)) {
zip_entry_open($zip);
$text = zip_entry_read($zip , zip_entry_filesize($zip));
zip_entry_close($zip);
}
print_r($text);
?>
I use this in my project:
function unzip_file( $data ) {
//save in tmp zip data
$zipname = "/tmp/file_xxx.zip";
$handle = fopen($zipname, "w");
fwrite($handle, $data);
fclose($handle);
//then open and read it.
$open = zip_open($zipname);
if (is_numeric($open)) {
echo "Zip Open Error #: $open";
} else {
while ($zip = zip_read($open)) {
zip_entry_open($zip);
$text = zip_entry_read($zip, zip_entry_filesize($zip));
zip_entry_close($zip);
}
}
/*delete tmp file and return variable with data(in this case plaintext)*/
unlink($zipname);
return $text;
}
I hope it helps you.