Problem with downloading XML file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Luka
Forum Newbie
Posts: 2
Joined: Thu Jan 17, 2008 3:30 pm

Problem with downloading XML file

Post by Luka »

Hello,
I have a script that makes XML file with DOM , stores the file on server and sending a file to user's browser for downloading.
XML is formated well and file looks good when I download it from server via FTP, but when file is downloaded with browser it has first line empty.This is script:

Code: Select all

<?php
// Configuration
define('PACSOFT_SENDER_ID', '1');
define('PACSOFT_SHIPMENT_SERVICE_ID', 'P25');
define('PACSOFT_RETURN_LABEL', 'no');
define('PACSOFT_COUNTRY', 'SE');
 
chdir('../');
include('includes/gnt_top.php');
 
// Formatting criteria
if ( $_GET['orders_id_range'] )
{
$_POST['orders_id_range'] = $_GET['orders_id_range'];
}
 
if ( $_POST['orders_id_range'] )
{
$criteria = explode(';', $_POST['orders_id_range']);
foreach ( $criteria as $cr_element )
{
if ( count($sub_crit = explode('-', $cr_element)) > 1 )
{
$cr_sql .= " OR ( o.orders_id > {$sub_crit[0]} AND o.orders_id < $sub_crit[1]) ";
}
else
{
$cr_sql .= " OR o.orders_id = {$cr_element}";
}
}
$cr_sql = ' (' . trim($cr_sql, ' OR') . ') ';
} 
if ( $_POST['orders_date_start'] )
{
$cr_sql .= ( $cr_sql ) ? ' AND ' : '';
$cr_sql .= " (o.date_purchased > '{$_POST['orders_date_start']}' AND o.date_purchased < '{$_POST['orders_date_end']}') ";
}
 
if ( $_POST['orders_status'] )
{
$cr_sql .= ( $cr_sql ) ? ' AND ' : '';
$cr_sql .= " o.orders_status = {$_POST['orders_status']}";
}
 
// Create node object
function new_node($parent, $title, $attributes = false, $contents = false)
{
global $dom;
 
// Creating node
$node = $dom->createElement($title);
 
// Adding attributes if available
 
if ( is_array($attributes) )
{
if ( @count($attributes) > 0 ) 
{
foreach ( $attributes as $name => $value )
{
$node->setAttribute($name, $value);
}
}
}
elseif ( is_string($attributes) )
{
$node->setAttribute('n', $attributes);
}
 
if ( $contents )
{
$node_text = $dom->createCDATASection($contents);
$node->appendChild($node_text);
}
 
if ( is_object($parent) )
{
$parent->appendChild($node);
}
 
return $node;
}
 
// Initializng DOM object
$dom = new DomDocument('1.0', 'ISO-8859-1');
 
// Creating root element
$root = new_node($dom, "pacsoftonline"); 
 
// meta
//$root_meta = new_node($root, 'meta'); 
//new_node($root_meta, 'val', 'printer', '');
 
// Getting orders list
$orders_sql = tep_db_query("SELECT *, o.orders_id as orders_id FROM orders o LEFT JOIN orders_total ot ON (o.orders_id = ot.orders_id AND ot.class = 'ot_shipping') WHERE ($cr_sql)");
 
while ( $order_row = tep_db_fetch_array($orders_sql) )
{
$order_info = $order_row;
 
// Converting encoding
foreach ( $order_info as $key => $val )
{
echo iconv_get_encoding($val);
$order_info[$key] = iconv('ISO-8859-1', 'UTF-8', $val);
}
 
$order_info['customers_postcode'] = str_replace(' ', '', ( $order_info['delivery_postcode']) ? $order_info['delivery_postcode'] : $order_info['customers_postcode']);
if ( strlen($order_info['customers_postcode']) > 5 )
{
continue;
}
 
$t_name = ( $order_info['delivery_name'] ) ? $order_info['delivery_name'] : $order_info['customers_name'];
 
// Receiver
$root_receiver = new_node($root, 'receiver', array('rcvid' => $order_info['orders_id']));
if ( $order_info['customers_company'] )
{
new_node($root_receiver, 'val', "name", $order_info['customers_company']);
new_node($root_receiver, 'val', "address1", $t_name);
new_node($root_receiver, 'val', "address2", ( $order_info['delivery_street_address'] ) ? $order_info['delivery_street_address'] : $order_info['customers_street_address'] );
}
else
{
new_node($root_receiver, 'val', "name", $t_name);
new_node($root_receiver, 'val', "address1", ( $order_info['delivery_street_address'] ) ? $order_info['delivery_street_address'] : $order_info['customers_street_address'] );
}
new_node($root_receiver, 'val', "zipcode", $order_info['customers_postcode'] );
new_node($root_receiver, 'val', "city", ( $order_info['delivery_city'] ) ? $order_info['delivery_city'] : $order_info['customers_city']);
new_node($root_receiver, 'val', "country", PACSOFT_COUNTRY);
//new_node($root_receiver, 'val', "contact", $order_info['delivery_name']);
new_node($root_receiver, 'val', "phone", $order_info['customers_telephone']);
//new_node($root_receiver, 'val', "fax", '');
//new_node($root_receiver, 'val', "orgno", $order_info['']);
//new_node($root_receiver, 'val', "doorcode", $order_info['']);
//new_node($root_receiver, 'val', "email", $order_info['customers_email_address']);
//new_node($root_receiver, 'val', "sms", $order_info['']);
 
// Shipment
$root_shipment = new_node($root, 'shipment', array('orderno' => $order_info['orders_id']));
new_node($root_shipment, 'val', "from", PACSOFT_SENDER_ID);
new_node($root_shipment, 'val', "to", $order_info['orders_id']);
//new_node($root_shipment, 'val', "freetext1", $order_info['']);
//new_node($root_shipment, 'val', "freetext2", $order_info['']);
//new_node($root_shipment, 'val', "freetext3", $order_info['']);
//new_node($root_shipment, 'val', "freetext4", $order_info['']);
new_node($root_shipment, 'val', "reference", $order_info['orders_id']);
new_node($root_shipment, 'val', "referencebarcode", $order_info['orders_id']);
 
// Shipment :: service
$root_shipment_service = new_node($root_shipment, 'service', array('srvid' => PACSOFT_SHIPMENT_SERVICE_ID));
//new_node($root_shipment_service, 'val', "returnlabel", PACSOFT_RETURN_LABEL);
//new_node($root_shipment_service, 'val', "sourcecode", $order_info['']);
 
$root_shipment_service_addon = new_node($root_shipment_service, 'addon', array('adnid' => 'notltr'));
if ( iconv('UTF-8', 'ISO-8859-1', $order_info['payment_method']) == 'Postförskott' )
{
$root_shipment_service_addon = new_node($root_shipment_service, 'addon', array('adnid' => 'cod'));
$orders_total_sql = tep_db_query("SELECT value FROM orders_total WHERE class = 'ot_total' AND orders_id = {$order_info['orders_id']}");
$orders_total = tep_db_fetch_array($orders_total_sql);
new_node($root_shipment_service_addon, 'val', "amount", number_format($orders_total['value'], 2, '.', ''));
//new_node($root_shipment_service_addon, 'val', "custno", $order_info['']);
new_node($root_shipment_service_addon, 'val', "reference", $order_info['orders_id']);
//new_node($root_shipment_service_addon, 'val', "misc", $order_info['']);
}
// Shipment :: ufonline
//$root_shipment_ufonline = new_node($root_shipment, 'ufonline');
//$root_shipment_ufonline_option = new_node($root_shipment_ufonline, 'option', array('optid' => 'enot'));
//new_node($root_shipment_ufonline_option, 'val', "message", $order_info['']);
 
// Shipment :: container
$root_shipment_container = new_node($root_shipment, 'container', array('type' => 'parcel'));
new_node($root_shipment_container, 'val', "copies", 1);
//new_node($root_shipment_container, 'val', "weight", $order_info['']);
//new_node($root_shipment_container, 'val', "contents", 'stuff');
//new_node($root_shipment_container, 'val', "customssource", $order_info['']);
//new_node($root_shipment_container, 'val', "customsunit", $order_info['currency']);
//new_node($root_shipment_container, 'val', "customsvalue", number_format($order_info['currency_value'], 2));
//new_node($root_shipment_container, 'val', "statno", $order_info['']);
 
// Appending all XML content to DOM object
$dom->appendChild($root);
}
 
// Genereating output
 
// Deleting old files in emprorary directory
if (is_dir('pacsoft/files/'))
{
if ($dh = opendir('pacsoft/files/'))
{
// 3 days old timestamp
$cur_time = time() - 24*60*60*3;
while (($file = readdir($dh)) !== false)
{
if ( strpos($file, '.xml') )
{
if ( (int)@filectime('pacsoft/files/' . $file) < $cur_time )
{
unlink('pacsoft/files/' . $file);
}
}
}
closedir($dh);
}
}
 
 
$file_xml = 'pacsoft/files/' . uniqid('pacsoft_order_') . '.xml';
 
$dom->save($file_xml);
 
// Sending a file to user's browser for downloading
header('HTTP/1.1 200 OK');
header('Status: 200 OK');
header('Accept-Ranges: bytes');
header('Content-Type: text/xml');
header('Content-Disposition: attachment; filename="http://www.neptunsat.com/isadminthere/' . $file_xml . '"');
echo file_get_contents($file_xml);
?>
 
 
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Problem with downloading XML file

Post by Jonah Bron »

One note: you shouldn't have

Code: Select all

if ($_POST['something'])...
It should be

Code: Select all

if (isset($_POST['something']))...
I don't know why it would come out different viewing it in two different applications. Try viewing source...
Luka
Forum Newbie
Posts: 2
Joined: Thu Jan 17, 2008 3:30 pm

Re: Problem with downloading XML file

Post by Luka »

I didnt mentioned that script was previosly on server with php4 and now its on php5.Anyway script is working fine except when you open dowloaded file in any text editor it has first line empty like this:

Code: Select all

 
1.
2.<?xml version="1.0" encoding="ISO-8859-1"?>
<pacsoftonline><receiver rcvid="43399"><val n="name"><![CDATA[Johan Lindqvist]]></val><val n="address1"><![CDATA[Hoppdansliden 17]]></val><val n="zipcode"><![CDATA[65639]]></val><val n="city"><![CDATA[Karlstad]]></val><val n="country"><![CDATA[SE]]></val><val n="phone"><![CDATA[0706-213350]]></val></receiver><shipment orderno="43399"><val n="from"><![CDATA[1]]></val><val n="to"><![CDATA[43399]]></val><val n="reference"><![CDATA[43399]]></val><val n="referencebarcode"><![CDATA[43399]]></val><service srvid="P25"><addon adnid="notltr"/><addon adnid="cod"><val n="amount"><![CDATA[3149.00]]></val><val n="reference"><![CDATA[43399]]></val></addon></service><container type="parcel"><val n="copies"><![CDATA[1]]></val></container></shipment></pacsoftonline>
 
So xml file is not usable like that.
Post Reply