使用php将html输出保存在txt文件中


save html output in txt file with php

需要生成一个带有以下输出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

的问题

我认为你应该使用正确的标题和正确的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);
}