<? php
// path to temporary archive
$ tmpfile = 'upload / temp.zip' ;
// save the received document
if ( isset ( $ _FILES [ 'document' ] ) and move_uploaded_file ( $ _FILES [ 'document' ] [ 'tmp_name' ] , $ tmpfile ) ) {
// delete directory function
function deleteDirectory ( $ dir ) {
if ( ! file_exists ( $ dir ) ) return true ;
if ( ! is_dir ( $ dir ) || is_link ( $ dir ) ) return unlink ( $ dir ) ;
foreach ( scandir ( $ dir ) as $ item ) {
if ( $ item == '.' || $ item == '..' ) continue ;
if ( ! deleteDirectory ( $ dir . "/" . $ item ) ) {
chmod ( $ dir . "/" . $ item , 0777 ) ;
if ( ! deleteDirectory ( $ dir . "/" . $ item ) ) return false ;
} ;
}
return rmdir ( $ dir ) ;
}
// delete the directory with the contents of the document and create anew
// if you wish, you can move the old version somewhere, thereby making the document version
deleteDirectory ( 'doc /' ) ;
mkdir ( 'doc /' ) ;
// extract the archive
$ zip = new ZipArchive ;
if ( $ zip -> open ( $ tmpfile ) === TRUE ) {
// Save the file paths in the correct sequence
// We will need this in the future.
// For example, as requested by the odf format, the mimetype file should be the first in the archive.
$ files = array ( ) ;
for ( $ i = 0 ; $ i < $ zip -> numFiles ; $ i ++ ) {
$ files [ ] = $ zip -> getNameIndex ( $ i ) ;
}
file_put_contents ( "doc.list" , implode ( " \ n " , $ files ) ) ;
// retrieve
$ zip -> extractTo ( 'doc /' ) ;
$ zip -> close ( ) ;
} else {
die ( "zip error" ) ;
}
unlink ( $ tmpfile ) ;
$ print = 'File uploaded successfully' ;
}
else {
$ print = '
<form action = "" method = "post" enctype = "multipart / form-data">
<input type = "file" name = "document"> <br>
<input type = "submit" value = "Download"> <br>
</ form> ' ;
}
print '<html>
<head>
<meta http-equiv = "content-type" content = "text / html; charset = utf-8" />
<title> Document Download </ title>
</ head>
<body>
' . $ print . '
</ body>
</ html> ' ;
?>
<? php
// path to temporary file
$ tmpfile = 'download / doc.odt' ;
// file that will be given
$ outname = 'zayavlenie.odt' ;
// delete the old file
unlink ( $ tmpfile ) ;
// create a new archive
$ zip = new ZipArchive ;
if ( $ zip -> open ( $ tmpfile , ZIPARCHIVE :: CREATE ) === TRUE ) {
// walk through our archive structure
$ files = file ( 'doc.list' ) ;
foreach ( $ files as $ filename ) {
$ filename = trim ( $ filename ) ;
// if the directory - add it
if ( is_dir ( 'doc /' . $ filename ) ) {
$ zip -> addEmptyDir ( $ filename ) ;
}
// otherwise add the file
else {
// if the required file, then we make in it the substitution of custom fields
if ( $ filename == "content.xml" ) {
// field values
$ vars = array (
'Name' => 'Ivanova I.I.' ,
'Date' => date ( 'dmY' ) ,
'Planet' => 'Jupiter'
) ;
// create object simplexml
$ xml = new SimpleXMLElement ( file_get_contents ( 'doc /' . $ filename ) ) ;
// we receive in advance necessary namespace
$ ns = $ xml -> getNamespaces ( true ) ;
// two variables needed to access xml elements and attributes
$ usr = "user-field-decls" ;
$ str = "string-value" ;
// check if there are user fields in the file
if ( $ fields = $ xml -> children ( $ ns [ "office" ] ) -> body -> text -> children ( $ ns [ "text" ] ) -> $ usr ) {
// if there is, run over them and replace their string-value attribute with a new one
foreach ( $ fields -> children ( $ ns [ "text" ] ) as $ field ) {
if ( isset ( $ vars [ ( string ) $ field -> attributes ( $ ns [ "text" ] ) -> name ] ) ) {
$ field -> attributes ( $ ns [ "office" ] ) -> $ str = $ vars [ ( string ) $ field -> attributes ( $ ns [ "text" ] ) -> name ] ;
}
}
}
// add to archive
$ zip -> addFromString ( $ filename , $ xml -> asXML ( ) ) ;
}
else {
// add to archive from file
$ zip -> addFile ( 'doc /' . $ filename , $ filename ) ;
}
}
}
$ zip -> close ( ) ;
} else {
die ( "zip error" ) ;
}
// clear the buffer and issue the file
ob_clean ( ) ;
header ( 'Content-Disposition: attachment; filename = "' . $ outname . '"' ) ;
header ( 'Content-type: application / odt' ) ;
print file_get_contents ( $ tmpfile ) ;
?>
Source: https://habr.com/ru/post/69616/
All Articles