Problem with downloading XML file

Posted by Luka, 01-16-2008, 12:00 PM
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 end of script: // 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="' . $file_xml . '"'); echo file_get_contents($file_xml); What might be a problem here because it used to work on other server?

Posted by stdunbar, 01-16-2008, 01:57 PM
Can you publish a link to look at the data? While I would usually expect a Content-Length field that isn't required and should not result in the behavior you're seeing.

Posted by Luka, 01-16-2008, 02:16 PM
I cant provide you a link because its in admin area of site (site of my client not mine) and needs logging, but this is xml output in browser: - - - - - - - - - - - - - - - - - - - - - - - - - - -

Posted by Luka, 01-16-2008, 02:18 PM
this is whole script: 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); ?>

Posted by foobic, 01-16-2008, 07:02 PM
That makes me think perhaps your server is parsing the xml file as php (short_open_tag On) but that doesn't make sense with the program you've posted, since file_get_contents shouldn't be parsing the xml file anyway. It might be useful to check the headers in your browser (Firefox plugin "Live HTTP headers" is good for this).

Was this answer helpful?

 Print this Article

Also Read

preventing hotlinking of images

Posted by AtoZ, 09-15-2007, 01:38 AMThe images I am trying to block are on page generated by a...

Any PHP library to create .ico? Or just to convert a PNG to ICO?

Posted by yangyang2036, 09-21-2008, 09:17 PMSearched google with nothing relevant. Posted by...

how to cron on windows server?

Posted by waveking, 05-03-2007, 10:04 AMHi, I truly love the 'cron' feature on Linux servers....

question about IP

Posted by wiredhosting, 07-17-2007, 11:16 AMHello everyone, i have 1 free IP in my dedicated...

Editting the default web page using SSH

Posted by MrDGOrman, 03-09-2010, 09:44 AMThis might be in the wrong section - if so, ma bad....