100高分请教了!!!解决后马上给分!!!急!!!

神鹰 2002-02-23 07:02:10
我这段加速页面传输的PHP代码,执行后为什么总是出现文件下载?解决后马上给分
<?
ob_start();
ob_implicit_flush (0);

function CompressPage () { //页面压缩
global $HTTP_ACCEPT_ENCODING;

if (strpos(' '.$HTTP_ACCEPT_ENCODING, 'gzip') && function_exists('gzcompress')) {
if (strpos(' '.$HTTP_ACCEPT_ENCODING, 'x-gzip')) {
$ENCODING = 'x-gzip';
} else $ENCODING = 'gzip';
} else $ENCODING = 0;

if ($ENCODING) {
print '<!-- CompressPage '.$ENCODING.' -->';
$contents = ob_get_contents(); ob_end_clean();

header("Content-Encoding: $ENCODING");
$size = strlen($contents);
$crc = crc32($contents);
$contents = gzcompress ($contents);
$contents = substr($contents, 0, strlen($contents)-4);

print pack('cccccccc',0x1f,0x8b,0x08,0x00,0x00,0x00,0x00,0x00); //print "\x1f\x8b\x08\x00\x00\x00\x00\x00";
print $contents;
print pack('V',$crc);
print pack('V',$size);
} else ob_end_flush();
}

print "<html>
<head>
<title>Test</title>
<META http-equiv=Content-Type content='text/html; charset=gb2312'>
</head>
<body>
OK!
</body>
</html>";

CompressPage();
?>
...全文
40 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
RobinCat 2002-02-27
  • 打赏
  • 举报
回复
搞定了没??
qsnake 2002-02-25
  • 打赏
  • 举报
回复
echo $ENCODING看看最后到底输出的是什么
amtd 2002-02-24
  • 打赏
  • 举报
回复
上面的$nozip为控制参数,可以先去掉不要。
amtd 2002-02-24
  • 打赏
  • 举报
回复
给你个例子:把你的代码加到中间省略处。
<?
if (function_exists("ob_start") and function_exists("crc32") and function_exists("gzcompress") and !$nozip){
ob_start();
}
?>
<html><head>
<meta content="text/html; charset=windows-1252" http-equiv="Content-Type">
<meta http-equiv="MSThemeCompatible" content="Yes">
<link rel="stylesheet" href="../include/style.css">
<body leftmargin="10" topmargin="10" marginwidth="10" marginheight="10">
.
.
.
.
</BODY></HTML>
<?php

if (function_exists("ob_start") and function_exists("crc32") and function_exists("gzcompress") and !$nozip) {
if (strpos(" ".$HTTP_ACCEPT_ENCODING,"x-gzip")) {
$encoding = "x-gzip";
}
if (strpos(" ".$HTTP_ACCEPT_ENCODING,"gzip")) {
$encoding = "gzip";
}

if ($encoding) {
$text = ob_get_contents();
ob_end_clean();

header("Content-Encoding: $encoding");

$size = strlen($text);
$crc = crc32($text);

$returntext = "\x1f\x8b\x08\x00\x00\x00\x00\x00";
$returntext .= substr(gzcompress($text,$level),0,-4);
$returntext .= pack("V",$crc);
$returntext .= pack("V",$size);

echo $returntext;
exit;
}
}
SimonDW 2002-02-24
  • 打赏
  • 举报
回复
header里加上输出:
Content-Type: text/html
神鹰 2002-02-24
  • 打赏
  • 举报
回复
多谢各位答复!这是一段将页面压缩书出的代码,所以使用了zlib库中的函数

zlib当然支持了,否则我这段代码汇报错的!而且我还使用了function_exists('gzcompress')监测这个压缩函数的存在。

问题是在浏览器这边,它不给压缩输出后的页面解压缩显示,而是当作了一个文件下载,问题可能是出在header("Content-Encoding: $ENCODING");函数里,大家再帮忙想想办法吧,真的很急,我试过无数方法都不行了。
amtd 2002-02-24
  • 打赏
  • 举报
回复
如果在linux中,编译php的时候要加参数“--with-zlib=yes”。这样php才可以支持gzip压缩输出。

如果在windows平台上,在php.ini中的extension中打开“;extension=php_zlib.dll”把分号去掉。如果失败,则下载php for windows的完全包。

如果不打开这个模块,当然就会出现你的问题了。
darzui 2002-02-23
  • 打赏
  • 举报
回复
你的代码我不是很懂,我觉得应该在apache的配置文件中注册mime类型
text/zip gzip
qsnake 2002-02-23
  • 打赏
  • 举报
回复
系统支持zlib么

21,891

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧