用PHP保存txt文件中的html输出

时间:2012-07-20 13:33:00

标签: php html file-io

需要使用以下输出html代码生成txt文件

<?php
function video() {
  $video = 'BYN-DEM7Mzw';
  echo '<script type="text/javascript">
    function youtubeFeedCallback( data ){
      document.writeln( data.entry[ "media$group" ][ "media$description" ].$t.replace( /\n/g, "<br/>" ) + "<br/>" ); }
    </script>
    <script type="text/javascript" src="http://gdata.youtube.com/feeds/api/videos/'.$video.'?v=2&amp;alt=json-in-script&amp;callback=youtubeFeedCallback"></script>';
}
ob_start();
video();
$output = ob_get_clean();
$desc = $output;
$video = 'BYN-DEM7Mzw';
$arq = $video.".txt";
$f = fopen($arq, "w+");
fclose;
$f = fopen($arq, "a+");
$content = "";
if(filesize($arq) > 0)
$content = fread($f, filesize($arq));
fwrite($f, $desc);
fclose($f);
?>

文件正在保存我的脚本而不是输出html

的问题

1 个答案:

答案 0 :(得分:0)

我认为你应该使用正确的headers和正确的mime

例如

header('Content-Description: File Transfer');
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename='.$file);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));

if ($file != '')
{ 
    echo file_get_contents($file);
}