Page 1 of 1

how do I save an XML file generated with PHP

Posted: Tue Mar 11, 2008 11:16 am
by cgoasduff
Hi There

I have a php file which connect and grab data from a mysql database.

If I call this PHP page it displays the content of the table in a xml format.. so far everything works fine.

I would like this php page to also create/overwrite an xml file and save it in a directory on the server.

I know I need to use something like that

Code: Select all

<?  
$file= fopen("myFile.xml", "w");
fwrite($file, not too sure what to put here);
fclose($file);
 ?>
 
but I have no idea where to put this in my code

Here is the page

Code: Select all

 
<?php
$hostname_conn = "localhost";
$database_conn = "image_gallery";
$username_conn = "root";
$password_conn = "";
$conn = mysql_pconnect($hostname_conn, $username_conn, $password_conn) or trigger_error(mysql_error(),E_USER_ERROR); 
?>
<?php
// Query the database and get all the records from the Images table 
mysql_select_db($database_conn, $conn);
$query_rsImages = "SELECT path,thumbpath,width,height,thumbwidth,thumbheight FROM images";
$rsImages = mysql_query($query_rsImages, $conn) or die(mysql_error());
$row_rsImages = mysql_fetch_assoc($rsImages);
$totalRows_rsImages = mysql_num_rows($rsImages);
// Send the headers
header('Content-type: text/xml');
header('Pragma: public');        
header('Cache-control: private');
header('Expires: -1');
?>
<gallery
base = ""
background = "#FFFFFF"
banner = "#F0F0F0"
text = "#000000"
link = "#0000FF"
alink = "#FF0000"
vlink = "#800080"
date = "4/11/2006">
    <sitename>China Gallery</sitename>
    <photographer></photographer>
    <contactinfo></contactinfo>
    <email></email>
    <security><![CDATA[]]> </security>
 
<banner font = "Arial" fontsize = "3" color =  "#F0F0F0"> </banner>
<thumbnail base ="thumbnails/" font = "Arial" fontsize = "4" color = "#F0F0F0" border = "0" rows = "3" col = "5"> </thumbnail> 
<large base ="images/" font = "Arial" fontsize = "3" color = "#F0F0F0" border = "0"> </large>
<photos id = "images">
  <?php if ($totalRows_rsImages > 0) { // Show if recordset not empty ?>
  
  <?php do { ?>
    <photo
    path="<?php echo $row_rsImages['path']; ?>"
    width = "<?php echo $row_rsImages['width']; ?>"
    height = "<?php echo $row_rsImages['height']; ?>"
    thumbpath="<?php echo $row_rsImages['thumbpath']; ?>"
    thumbwidth = "<?php echo $row_rsImages['thumbwidth']; ?>"
    thumbheight = "<?php echo $row_rsImages['thumbheight']; ?>"
    >       
    </photo>
    <?php } while ($row_rsImages = mysql_fetch_assoc($rsImages)); ?>
    <?php } // Show if recordset not empty ?>
</photos>
</gallery>
 
<?php
 
mysql_free_result($rsImages);
?>
 
Thank you

Christophe

Re: how do I save an XML file generated with PHP

Posted: Tue Mar 11, 2008 4:52 pm
by pickle
You're echoing the XML data to the screen - it's not stored in a string variable anywhere (it will need to be for you to write it to a file). Your options as I see it are:
  • Call ob_start() before your current output, and ob_get_clean() after your current output to capture what's being echoed.
  • Don't echo the xml at all - store it in a string that you can then save in a file AND echo to the screen.
If you're running PHP5, file_put_contents() works easier than fopen(),fwrite(),fclose()