views:

53

answers:

1

Hi I have a script that creates a file and stores it on the server. The file is encoded in UTF-8 and is a kind of xml file for the cmap software.

If i open the file directly from the server then there is no problem and the file can be read.

I am forcing a download of this file when a user goes to a specific url. After such a download, the file is unreadable by the cmap software. I have to go into my text editor (notepad++) and change the encoding from UTF-8 to UTF-8 without BOM.

Am I sending the wrong headers? Is php doing something to the file when it is downloading it?

Any advice on this would really be appreciated.

Cheers Drew

EDIT

Sorry, there is a lot of code involved in a couple of different classes. I have included the code i am using to send the file to the browser:

function exportCMAP()
{   
    $serializer = new Serializer();
    $serializer->serializeCmap();

    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=export.cxl");
    header("Content-Type: x-cmap/text-xml");
    header("Content-Transfer-Encoding: binary");

    readfile("temp/export.cxl");
}

If the code that generates the xml (using XMLWriter) is required i can post that too.

EDIT

As requested here is the code where the xml is being produced - it is in another class:

        function serializeCmap()
    {

        $storeManager = new StoreManager();         
        $linkedNodes = $storeManager->getLinkedNodes();

        $namespaces = Array();

        $writer = new XMLWriter();

        $writer->openMemory();
        $writer->setIndent(4); 

        $writer->startDocument('1.0', 'utf-8');

            $writer->startElement('cmap');

                $writer->writeAttribute('xmlns', 'http://cmap.ihmc.us/xml/cmap/');
                $writer->writeAttribute('dc', 'http://purl.org/dc/elements/1.1/');

            $writer->startElement('res-meta');

                $writer->writeElement("dc:title", "Full schema for Cmap");
                $writer->writeElement("dc:description", "Description Goes Here");

            $writer->endElement();  

            $writer->startElement('map');

                $writer->startElement('concept-list');

                    foreach($linkedNodes['nodes'] as $node=>$id) {

                        $writer->startElement('concept');

                            $writer->writeAttribute("id", $id);
                            $writer->writeAttribute("label", $node);

                        $writer->endElement();
                    }

                $writer->endElement();

                $writer->startElement('linking-phrase-list');

                    foreach($linkedNodes['phrases'] as $phrase=>$id) {

                        $writer->startElement('linking-phrase');

                            $writer->writeAttribute("id", $id);
                            $writer->writeAttribute("label", $phrase);

                        $writer->endElement();
                    }

                $writer->endElement();

                $writer->startElement('connection-list');

                    foreach($linkedNodes['connections'] as $key=>$val) {

                        $writer->startElement('connection');

                            $writer->writeAttribute("from-id", $val['from']);
                            $writer->writeAttribute("to-id", $val['phrase']);

                        $writer->endElement();

                        $writer->startElement('connection');

                            $writer->writeAttribute("from-id", $val['phrase']);
                            $writer->writeAttribute("to-id", $val['to']);

                        $writer->endElement();
                    }

                $writer->endElement();

            $writer->endElement();

        $writer->endElement();

        $writer->endDocument();

        file_put_contents("temp/export.cxl",$writer->outputMemory());
    }
+1  A: 

You should add two important things:

  1. Test if the HTTP header has not been sent yet, and
  2. ensure that there is no more output than the one of readfile.

So try this:

function exportCMAP() {
    if (headers_sent()) {
        // HTTP header has already been sent
        return false;
    }
    // clean buffer(s)
    while (ob_get_level() > 0) {
        ob_end_clean();
    }
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=export.cxl");
    header("Content-Type: x-cmap/text-xml");
    header("Content-Transfer-Encoding: binary");
    readfile("temp/export.cxl");
    // avoid any further output
    exit;
}
Gumbo
Great thanks, thats worked! I have spent around 4 hours on this problem today! I had tried checked whether the headers had been sent but that wasnt the issue - It was the clean buffers that has solved it. I have no idea what the actual issue was but am really glad it is working now. Would you suggest always including that code when sending files for download?
Drew