php - 使用 PHP gzipp 在服务器上保存 gzip 压缩的 html 文件

标签 php html gzip save

我使用此脚本自动将 PHP 文件保存到服务器上的 HTML 文件中:

<?php
$page=file_get_contents('http://yourdomain.com/index.php');
$fp=fopen('index.html','w+');
fputs($fp,$page);
fclose($fp);
?>

我怎样才能让服务器在服务器上保存index.php的Gzipped版本?

目标是在文件夹中包含以下文件:

  • 将indexphp-into-indexhtml-and-indexgz.php

  • index.php

  • index.html

  • index.gz

最佳答案

如果您只能通过 URL 访问该页面(即它不是您自己服务器上的文件),正如一些评论者所指出的那样 - 您将只能访问 PHP/ASP/Whatever 的输出文件。

如果不是本地文件

<?php
$theUrl = 'http://www.server.com/filename.htm';
$theOutput = file_get_contents( $theUrl );
$outBase = preg_replace( '/[^\d\w]/' , '_' , $theUrl );
// Output standard HTML file (Filename may be clunky, but, up to you to fix that)
$outHTM = $outBase.'.htm';
file_put_contents( $outHTM , $theOutput );
// Output a GZipped version of same
$outGZ = $outBase.'.htm.gz';
$theOutput = gzencode( $theOutput , 9 );
file_put_contents( $outGZ , $theOutput );
?>

如果是本地文件,那么就可以进行上述操作,而且...

<?php
$theLocalFile = '/somefolder/anotherfolder/index.php';
$theLocalContent = file_get_contents( $theLocalFile );
$localBase = preg_replace( '/[^\d\w]/' , '_' , $theLocalContent );
// Save an uncompressed copy
$outLocal = $localBase.'.php';
file_put_contents( $outLocal , $theLocalContent );
// Save a GZipped copy
$outGZ = $localBase.'.php.gz';
$theOutput = gzencode( $theLocalContent , 9 );
file_put_contents( $outGZ , $theOutput );
?>

关于php - 使用 PHP gzipp 在服务器上保存 gzip 压缩的 html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5175359/

相关文章:

php - 检查函数是否需要参数

php - mpdf - 第一页上的文本延伸到第二页,与页眉重叠

javascript - 刷新页面时标题外观不起作用

javascript - 使用提交按钮而不是 anchor 标记打开新链接

PHP:将 Foreach 数据值传递给输入框,同时增加其 ID 和名称的数量

javascript - 在 osTicket 中购买票证花费了多少小时?

javascript - 如何根据增加/减少值制作仪表动画

php - 如何通过 AFNetworking API 调用使用 gzip 解压缩?

python - 想要为两个文件添加时间戳

encoding - 变化 : Accept-Encoding overkill?