2009-09-08 4 views
3

XML 파일을 화면에 인쇄하는 스크립트를 작성했지만 다운로드 대화 상자를 열어서 파일로 저장할 수 있습니다.PHP XML- 파일로 내보내기

어떻게 할 수 있습니까?

thnx!

스크립트 :

<?php 
print '<?xml version="1.0" encoding="UTF-8" ?>'; 
print "\n <data>"; 
... 
print "\n </data>"; 
?> 
+1

하지 질문에 대한 답변,하지만 난 그것을 '수동'(예를 들어, SimpleXML을 구축하는 대신 XML을 생성하는 일부 라이브러리/클래스를 사용하는 것이 좋습니다 것 - 사용 아주 자주 드루팔 (Drupal) 코드 또는 DOMDocument (더 복잡한 요구 사항이있는 경우). 이렇게하면 많은 작업을 절약 할 수 있습니다. –

+0

@headkit :이를 위해 모듈을 작성한 경우. 나에게 코드를 알려 주실 수 있겠습니까? 답장을 보내 주시면 이메일 ID로 알려 드리겠습니다. 감사합니다 – Cindrella

+0

모듈을 만들지 못했습니다. 죄송합니다. – headkit

답변

4

바로 헤더를 설정하려고 :

<?php 
header('Content-Type: text/xml'); 
header('Content-Disposition: attachment; filename="example.xml"'); 
header('Content-Transfer-Encoding: binary'); 

print '<?xml version="1.0" encoding="UTF-8" ?>'; 
print "\n <data>"; 
... 
print "\n </data>"; 
?> 
+0

이것은 내가 좋아하는 것이지만 작동하지 않습니다. .- ( – headkit

+1

) 헤더 선언 앞에'ob_end_clean();'을 다시 쓰면됩니다. 태그에서 Drupal 컨텍스트에서 발생한다고 가정하고 Drupal은 이미 일부 헤더를 미리 설정하여 간섭 할 수 있습니다. –

+0

헤더가 이미 전송되었다는 경고 메시지가 표시됩니까?/var/log/apache2 /에있는 아파치 로그를 살펴보십시오.이 경우 Henrik의 제안을 시도해 볼 수 있습니다. – stefita

3

하여 표시하도록 브라우저를 강제로 다음 사용해보십시오 "다른 이름으로 저장을 ..."대화 : 브라우저 해석/표시 방법을 알지 못하거나 내용이 헤더에 지시 된 경우 내용 유형에 대한 "다른 이름으로 저장 ..."대화 상자를 보여줍니다. 올바른 헤더를 알아두면 다운로드하도록 지정하고, 기본 파일 이름, 내용 유형 및 캐시 방법을 지정할 수 있습니다.

<?php 
$xml = '<?xml version="1.0" encoding="UTF-8" ?>'; 
$xml .= "\n <data>"; 

// Create the rest of your XML Data... 

$xml .= "\n </data>"; 
downloader($xml, 'yourFile.xml', 'application/xml'); 

기능 코드 :

<?php 
if(!function_exists('downloader')) 
{ 
    function downloader($data, $filename = true, $content = 'application/x-octet-stream') 
    { 
    // If headers have already been sent, there is no point for this function. 
    if(headers_sent()) return false; 
    // If $filename is set to true (or left as default), treat $data as a filepath. 
    if($filename === true) 
    { 
     if(!file_exists($data)) return false; 
     $data = file_get_contents($data); 
    } 
    if(strpos($_SERVER['HTTP_USER_AGENT'], "MSIE") !== false) 
    { 
     header('Content-Disposition: attachment; filename="'.$filename.'"'); 
     header('Expires: 0'); 
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
     header('Content-Transfer-Encoding: binary'); 
     header('Content-Type: '.$content); 
     header('Pragma: public'); 
     header('Content-Length: '.strlen($data)); 
    } 
    else 
    { 
     header('Content-Disposition: attachment; filename="'.$filename.'"'); 
     header('Content-Transfer-Encoding: binary'); 
     header('Content-Type: '.$content); 
     header('Expires: 0'); 
     header('Pragma: no-cache'); 
     header('Content-Length: '.strlen($data)); 
    } 
    // Send file to browser, and terminate script to prevent corruption of data. 
    exit($data); 
    } 
} 
확실히