下载后PHP文件不可读

时间:2010-04-18 16:40:09

标签: php encoding header byte-order-mark

您好我有一个脚本可以创建一个文件并将其存储在服务器上。该文件以UTF-8编码,是cmap软件的一种xml文件。

如果我直接从服务器打开文件,那么没有问题,可以读取文件。

当用户访问特定网址时,我强制下载此文件。下载完成后,cmap软件无法读取该文件。我必须进入我的文本编辑器(记事本++)并将编码从UTF-8更改为UTF-8而不使用BOM。

我发错了标题吗? php在下载文件时是否对文件做了什么?

对此有任何建议真的很感激。

干杯 德鲁

修改

很抱歉,有几个不同的类涉及很多代码。我已经包含了用于将文件发送到浏览器的代码:

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");
}

如果需要生成xml的代码(使用XMLWriter),我也可以发布。

修改

这里要求的是生成xml的代码 - 它在另一个类中:

        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 个答案:

答案 0 :(得分:3)

你应该添加两件重要的事情:

  1. 测试是否尚未发送HTTP标头,
  2. 确保输出的输出不会超过readfile
  3. 的输出

    所以试试这个:

    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;
    }