最近公司有个需求要求在下载的附件中加入公司的广告,公司的附件主要时zip和rar文件。一共大概有接近200万个,老板让我写一个脚本去在这些附件中添加广告。 我大概估计了一下。这样做可能不太好。于是想通过在下载时把附件和广告打成一个包处理。上网搜索了一下nginx,有一个mod_zip模块可以实现这个功能,在安装时 遇到一些问题,但是使用还是比较简单的,不需要对nginx进行任何配置,只需要php添加相应到header就可以了。
下载mod_zip模块
git clone
配置编译
./configure --user=www --group=www --add-module=../ngx_cache_purge-2.1 --prefix=/usr/local/webserver/nginx144 --with-http_stub_status_module --with-http_ssl_module --with-pcre --with-openssl=../openssl-1.0.1g --add-module=../mod_zip make编译时可能会有下面的错误报错 ../mod_zip/ngx_http_zip_file.c: 在函数‘ngx_http_zip_generate_pieces’中: ../mod_zip/ngx_http_zip_file.c:331: 错误:‘else’前需要‘}’ cc1: warnings being treated as errors ../mod_zip/ngx_http_zip_file.c:202: 错误:未使用的变量‘cd_piece’ ../mod_zip/ngx_http_zip_file.c: 在文件层: ../mod_zip/ngx_http_zip_file.c:389: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token ../mod_zip/ngx_http_zip_file.c:391: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token ../mod_zip/ngx_http_zip_file.c:392: 错误:expected identifier or ‘(’ before ‘if’ ../mod_zip/ngx_http_zip_file.c:396: 错误:数据定义时没有类型或存储类 ../mod_zip/ngx_http_zip_file.c:396: 错误:在‘cd_piece’的声明中,类型默认为‘int’ ../mod_zip/ngx_http_zip_file.c:396: 错误:‘ctx’未声明(不在函数内) ../mod_zip/ngx_http_zip_file.c:396: 错误:‘piece_i’未声明(不在函数内) ../mod_zip/ngx_http_zip_file.c:397: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token ../mod_zip/ngx_http_zip_file.c:398: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token ../mod_zip/ngx_http_zip_file.c:399: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token ../mod_zip/ngx_http_zip_file.c:401: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token ../mod_zip/ngx_http_zip_file.c:403: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token ../mod_zip/ngx_http_zip_file.c:405: 错误:expected identifier or ‘(’ before ‘return’ ../mod_zip/ngx_http_zip_file.c:406: 错误:expected identifier or ‘(’ before ‘}’ token make[1]: *** [objs/addon/mod_zip/ngx_http_zip_file.o] 错误 1 make[1]: Leaving directory `/data/soft/installnginx144/nginx-1.4.4' make: *** [build] 错误 2
修改源文件 vim ../mod_zip/ngx_http_zip_file.c 到331行改为}else #endifif(vv->len) { 修改makefile文件 vim objs/Makefile 删除 -Werror编译 make && make install 这时应该可以编译成功了
安装好以后只需修改php文件即可实现功能
//如果是本地文件
<?phpheader('X-Archive-Files: zip');header('Content-Type: application/octet-stream');header('X-Accel-Chareset: utf-8');header('Content-Disposition: attachment; filename="test.zip"');echo "1a6349c5 24 /
5d70c4d3 25 / ";
?>//如果文件大小不确定可以用函数获取文件大小
function getFileSize($url) {$url = parse_url($url);if(!!$fp = @fsockopen($url['host'],empty($url['port'])?80:$url['port'],$error)) {fputs($fp,"GET ".(empty($url['path'])?'/':$url['path'])." HTTP/1.1rn");fputs($fp,"Host:$url[host]rnrn");while(!feof($fp)) {$tmp = fgets($fp);if(trim($tmp) == '') break;if(preg_match('/Content-Length:(.*)/si',$tmp,$arr)) return trim($arr[1]);}return null;} else {return null;}header('Content-Disposition: attachment; filename="'.$attachname.'"');
header('Content-type: '.$fileext);
header('X-Archive-Files: zip');
$Len=getFileSize('/'.$filename);
echo '5d70c4d3 25 /
- '.$Len.' /'.$filename.' '.$attachname.'.'.$fileext;
安装这个插件最大到启示就是不要迷信开源的代码里不会有简单到错误,的确遇到了就大胆到修改一下。
本文发布于:2024-02-04 08:39:31,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170703302054034.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |