CodeCharge Studio
search Register Login  

Visual PHP Web Development

Visually Create Internationalized Web Applications, Web Reports, Calendars, and more.
CodeCharge.com

YesSoftware Forums -> CodeCharge Studio -> PHP

 Write Output to File

Print topic Send  topic

Author Message
damian

Posts: 838
Posted: 01/27/2013, 5:28 PM

Hi Guys,
I would like to export or write the output of a page to a file for download.
Situation - a game I play uses XML modules. I want to allow multiple people to contribute data to this module. Create simple Add/Edit form to allow contributions.
Create a page that displays the correctly formatted XML - no table tags, html tags etc.
How do I get this content to write to a file that I can then download?
Any ideas?
Damian

_________________
if you found this post useful take the time to help someone else.... :)
View profile  Send private message
damian

Posts: 838
Posted: 01/28/2013, 7:44 AM

Ok... so this is what I did...

I created my page in CCS with 2 tables etc... stripped out all html coding leaving only <!-- Begin and End --> tags and labels.
I added all the xml before and inside tags as appropriate.
on calling this page it generates my required xml content - in that page - myexport.php

i created a new file with text editor called doexport.php containing the following key bits:


function to create ZIP file (sourced from http://davidwalsh.name/create-zip-php)[/b]

  
/* creates a compressed zip file */  
function create_zip($files = array(),$destination = '',$overwrite = false) {  
  //if the zip file already exists and overwrite is false, return false  
  if(file_exists($destination) && !$overwrite) { return false; }  
  //vars  
  $valid_files = array();  
  //if files were passed in...  
  if(is_array($files)) {  
    //cycle through each file  
    foreach($files as $file) {  
      //make sure the file exists  
      if(file_exists($file)) {  
        $valid_files[] = $file;  
      }  
    }  
  }  
  //if we have good files...  
  if(count($valid_files)) {  
    //create the archive  
    $zip = new ZipArchive();  
    if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {  
      return false;  
    }  
    //add the files  
    foreach($valid_files as $file) {  
      $zip->addFile($file,$file);  
    }  
    //debug  
    //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;  
      
    //close the zip -- done!  
    $zip->close();  
      
    //check to make sure the file exists  
    return file_exists($destination);  
  }  
  else  
  {  
    return false;  
  }  
}  

code to delete old files - just in case

unlink('db.xml');  
unlink('cc-npc.zip');

create new db.xml file from myexport.php - this is the output to file bit

// Begin updating db.xml  
  
$myFile = "db.xml";  
$fh = fopen($myFile, 'w') or die("can't open file");  
  
$content = "http://fqdn/myexport.php";  
$myxml = implode("", file($content));  
  
fwrite($fh, $myxml);  
fclose($fh);  

nominate files for the zip and create the zip file

$files_to_zip = array(  
  'definition.xml',  
  'db.xml',  
  'thumbnail.png',  
);  
  
//if true, good; if false, zip creation failed  
$result = create_zip($files_to_zip,'cc-npc.zip');

end of code stuff


then some html output etc to link to the new zip file for download...

hth someone :)



_________________
if you found this post useful take the time to help someone else.... :)
View profile  Send private message
yourguide


Posts: 44
Posted: 02/08/2013, 12:28 PM

Cool... this does help.
Thanks!

_________________
Independent, self-employed, web developer.
View profile  Send private message
Lucius

Posts: 220
Posted: 02/11/2013, 6:05 AM

Quote :
I created my page in CCS with 2 tables etc... stripped out all html coding leaving only <!-- Begin and End --> tags and labels. I added all the xml before and inside tags as appropriate. on calling this page it generates my required xml content - in that page - myexport.php

Sorry but that's a bad way to do it unfortunately. I learned it the hard way...

It seems a good idea, until you create large XML this way - it will be very very inefficient and most likely you will encounter timeouts, server out of memory messages and so on...

Use XML DOM, or other PHP interface for creating XML documents. This will also validate your XML on the fly, which is missing in your current solution.

Simple example for XML DOM: http://www.php.net/manual/pl/domdocument.createelement.php


Also if you need you can easily directly output XML as a file in your browser request like so:

  
//uncompressed content  
	    $uncompressed = $dom->saveXML();               
	      
	    //compress to gzip archive  
	    $out = gzencode($uncompressed,6);   
	      
	    //output to browser                                
		header("Content-Description: File Transfer");  
		header("Content-Type: application/force-download");  
		header("Content-Type: application/zip");  
		header('Content-Length: '.strlen($out));   
		header('Content-Disposition: attachment; filename="your_xml_file.xml.gz"');  
		header('Cache-Control: no-cache, no-store, max-age=0, must-revalidate');  
		header('Pragma: no-cache');  

If you modify the headers, you can instead of download display it directly in browser (no need to link to a file).

  
        //raw output XML  
    	header("Content-type: text/xml");  
    	echo $dom->saveXML();  
View profile  Send private message
damian

Posts: 838
Posted: 02/11/2013, 7:28 AM

Hi Lucius,

Thank you for the response.
At what size (in your experience) did you start having issues with this?

In my situation I want to actually create a zip - its not a workaround - that is the output I require. The zip also contains a subdirectory which contains a small image file for many of the records.

If my document is validated are there other advantages of your method?

regards
Damian

_________________
if you found this post useful take the time to help someone else.... :)
View profile  Send private message
Lucius

Posts: 220
Posted: 02/11/2013, 9:01 AM

Hi,

Server went nuts, when I tried to output about 1MB of XML uncompressed data, I didn't check what is the threshold for this, could be much lower...

If you have simple, small XML it will be ok. Also how often it would be use is a factor.

Generally if you grasp the XML DOM (takes little time, even there are many available functions, you really need 3-4 of them), creating your XMLs is as easy as the way you describe. Right now I use it even for simple XML documents (reliability, ease of use).

View profile  Send private message

Add new topic Subscribe to topic   


These are Community Forums for users to exchange information.
If you would like to obtain technical product help please visit http://support.yessoftware.com.

PHP Reports

Visually create Web Reports in PHP, ASP, .NET, Java, Perl and ColdFusion.
CodeCharge.com

Home   |    Search   |    Members   |    Register   |    Login


Powered by UltraApps Forum created with CodeCharge Studio
Copyright © 2003-2004 by UltraApps.com  and YesSoftware, Inc.