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

发布于 2024-10-19 18:41:27 字数 476 浏览 6 评论 0原文

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

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

How can I get the server to save a Gzipped version of index.php on the server?

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

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

  • index.php php

  • index.html

  • index.gz

I use this script to automatically save a PHP-file into a HTML file on my server:

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

How can I get the server to also save a Gzipped version of index.php on the server?

The goal is to have the following files in the folder:

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

  • index.php

  • index.html

  • index.gz

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ら栖息 2024-10-26 18:41:27

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

如果它不是本地文件

<?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 );
?>

If you can only access the page by URL (ie. it is not a file on your own server), as pointed out by a few commenters - you will only be able to access the output of the PHP/ASP/Whatever file.

If it is not a Local File

<?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 );
?>

If it is a local file, then you can do the above, but also...

<?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 );
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文