您好我有一个脚本可以创建一个文件并将其存储在服务器上。该文件以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());
}
答案 0 :(得分:3)
你应该添加两件重要的事情:
readfile
。所以试试这个:
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;
}