PHP生成text并下载,生成的文件中包含整个页面HTML代码解决方法

近日在写一个导入导出功能时发现生成的文件中不仅包含了要导出的字符,还把整个页面的HTML一同导出了,下面是解决方案:

function wysj_supermenu_export(){
     
	$string = "example";
	$today = getdate();
    $today_str = $today['year'].'-'.$today['mon'].'-'.$today['mday'];
    $export_file = '5usujian-super-menu-settings-'.$today_str.'.txt';
	header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=" . urlencode($export_file));
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header("Pragma: no-cache");
    header("Expires: 0");

    print $string;
    // 重点!必须要加die()
    die();
}

最后必须加die(),否则会将整个页面内容,包含HTML等等,全部生成在text文件中。

你可能感兴趣的:(wordpress,PHP,php生成txt)