【问题标题】:Save a gzipped html file on server by using PHP gzipp使用 PHP gzipp 在服务器上保存 gzip 压缩的 html 文件
【发布时间】:2011-07-07 17:12:19
【问题描述】:

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

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

如何让服务器同时在服务器上保存一个 Gzipped 版本的 index.php?

目标是在文件夹中有以下文件:

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

  • index.php

  • index.html

  • index.gz

【问题讨论】:

  • 请记住,您无法获取 php 文件的实际来源,因为引用 yourdomain.com/index.php 会产生代码的处理输出,例如HTML 或者它可能是什么。
  • 我不确定我理解你的意思。你是说我的服务器上无法使用 PHP 保存一个 Gzipped 版本的 index.php 吗?
  • 你会得到文件的输出,而不是源文件。

标签: php html gzip save


【解决方案1】:

如果您只能通过 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 );
?>

【讨论】:

  • 感谢卢卡诺斯为我指明了正确的方向。 gzencode() 为我工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-07
  • 1970-01-01
  • 1970-01-01
  • 2020-04-07
  • 2011-08-08
相关资源
最近更新 更多