如何在yii中下载xml文件

时间:2013-11-13 07:38:10

标签: php xml yii header readfile

我想在yii中创建和下载xml文件所以我已经编写了代码来创建xml文件并且它工作正常但是在下载xml文件时出错了我已经尝试了代码

//xml structure
$xmldata = '<?xml version="1.0" encoding="utf-8"?>';
$xmldata .= '<MemberBill>';
foreach ($model as $model)
{
    $xmldata .= '<Bill>';
    $xmldata .= '<BillNo>'.$model->bill_no.'</BillNo>';
    $xmldata .= '</Bill>';
}
$xmldata .= '</MemberBill>';

if(file_put_contents('memberBill.xml',$xmldata)) // this code is working fine xml get created
{
    //echo "file created";exit;
    header('Content-type: text/xml');   // i am getting error on this line
    //Cannot modify header information - headers already sent by (output started at D:\xampp\htdocs\yii\framework\web\CController.php:793)

    header('Content-Disposition: Attachment; filename="memberBill.xml"');
    // File to download
    readfile('memberBill.xml');        // i am not able to download the same file
}

1 个答案:

答案 0 :(得分:1)

D:\xampp\htdocs\yii\framework\web\CController.php:793代码中已经开始输出 - 它是方法render。我想你已经在这段代码之前做了一些事情!