1

我有以下函数从数据库中获取一个 blob 并应该将文件返回到浏览器,但是它返回一个损坏的文件:

$file_data是一个数组,其中包含从文件表中返回的行,其中包含 blob、内容类型、上次修改时间和其他此类内容。

$datablob$file_data 数组的组成部分。

function header_file($data, $file_data)
            {
                $last_modified = gmdate('D, d M Y H:i:s', $file_data['unix_last_modified_time'])." GMT";


                // if browser question if it's up to date
                if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE']))
                {
                    // parse header
                    $if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);

                    if ($if_modified_since == $last_modified)
                    {
                        // the browser's cache is still up to date
                        header("HTTP/1.0 304 Not Modified");
                        header("Cache-Control: max-age=86400, must-revalidate");
                        exit;
                    }
                }

                header("Cache-Control: max-age=86400, must-revalidate");
                header("Last-Modified: ".$last_modified);
                header("Content-Type: ".$file_data['file_upload_type']);
                // this prevents caching...
                // yea, lots of hair lost to this one...
                //header("Content-Length: " . strlen($data));
                header("Content-Transfer-Encoding: binary");

                if($file_data['file_upload_type'] == 'application/x-shockwave-flash')
                    header("Content-Disposition: inline; filename=\"".str_replace(' ','_',$file_data['file_upload_name'])."\"");
                else
                    header("Content-Disposition: attachment; filename=\"".str_replace(' ','_',$file_data['file_upload_name'])."\"");

                // send data to output
                echo $data;
                exit;
            }

在函数运行之前,输出缓冲区被清除:

if(ob_get_length() > 0) 
            {
                ob_clean();
            }

结果:

文件下载,文件大小正确,但已损坏。相关问题:https ://stackoverflow.com/questions/19768650/zend-caching-of-images-gives-problems-once-the-site-goes-down-for -一会儿

回复:

Request URL:http://www.example.com/index.php?module=uploads&sub_module=getfile&id=4982
Request Method:GET
Status Code:200 OK
Request Headersview source
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Cookie:banner89=yes; banner90=yes; PHPSESSID=vhlk92ihtcdmtv2q4vhjbmsv54; __utmz=45276912.1383308583.8.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none); __utma=45276912.1890999283.1366697926.1383574554.1383631349.15; __utmc=45276912
DNT:1
Host:www.example.com
Pragma:no-cache
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Query String Parametersview sourceview URL encoded
module:uploads
sub_module:getfile
id:4982
Response Headersview source
Cache-Control:max-age=86400, must-revalidate
Connection:keep-alive
Content-Disposition:attachment; filename="Masthead-Banner.gif"
Content-Encoding:gzip
Content-Length:5920
Content-Type:image/gif
Date:Tue, 05 Nov 2013 09:14:03 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Last-Modified:Tue, 05 Nov 2013 08:27:55 GMT
Pragma:no-cache
Server:Apache
Vary:Accept-Encoding
X-Cache:MISS from firewall.

更多调查:

打开下载的文件并与服务器上的文件进行比较,发现文本的页眉和页脚相同,但有很多不同的字符:

实物:

%PDF-1.5
%âãÏÓ
165 0 obj
<</Linearized 1/L 100758/O 167/E 86272/N 4/T 100409/H [ 498 245]>>
endobj

181 0 obj
<</DecodeParms<</Columns 5/Predictor 12>>/Filter/FlateDecode/ID[<47941C1B25C34A4EA92EE88606328B32><09EC517E475E964EB1CBEF770BC3C54D>]/Index[165 33]/Info 164 0 R/Length 91/Prev 100410/Root 166 0 R/Size 198/Type/XRef/W[1 3 1]>>stream
hÞbbd```b``º"§I~É"Á²`Ĺ,«æ*H®(;D&L@ÿí¿Hþßø  h   XÊäF¯O  ~O
7
endstream
endobj
startxref
0
%%EOF

197 0 obj
<</C 163/Filter/FlateDecode/I 185/Length 151/O 147/S 94>>stream
hÞb```¢vV3A Ç%êzů K    ULT«Ú1Q5}ukGGGGFGG#Ã#>(f`dàg¬áú¨}À!óÆÄF ?O1x7°30ínÒ@ôHs00íÍa ;Sn$Óu¨*E  »!S
endstream
endobj
166 0 obj
<</MarkInfo<</Marked true>>/Metadata 10 0 R/Outlines 14 0 R/PageLayout/OneColumn/Pages 163 0 R/StructTreeRoot 25 0 R/Type/Catalog>>
endobj
167 0 obj
<</Contents 171 0 R/CropBox[0.0 0.0 612.0 792.0]/MediaBox[0.0 0.0 612.0 792.0]/Parent 163 0 R/Resources<</ColorSpace<</CS0 182 0 R/CS1 183 0 R>>/Font<</C2_0 188 0 R/TT0 190 0 R/TT1 192 0 R/TT2 194 0 R/TT3 196 0 R>>>>/Rotate 0/StructParents 0/Type/Page>>
endobj
168 0 obj
<</Filter/FlateDecode/First 121/Length 1269/N 15/Type/ObjStm>>stream

损坏:

%PDF-1.5
%âãÏÓ
165 0 obj
<</Linearized 1/L 100758/O 167/E 86272/N 4/T 100409/H [ 498 245]>>
endobj

181 0 obj
<</DecodeParms<</Columns 5/Predictor 12>>/Filter/FlateDecode/ID[<47941C1B25C34A4EA92EE88606328B32><09EC517E475E964EB1CBEF770BC3C54D>]/Index[165 33]/Info 164 0 R/Length 91/Prev 100410/Root 166 0 R/Size 198/Type/XRef/W[1 3 1]>>stream
hÞbbd```b``º"§‚I~É"™Á²`Ĺ,«æ*ƒH®(™;D&L‘@’‘ÿˆí¿Hþßø™ h‹  X–‘ÊäF¯O  ~O
7
endstream
endobj
startxref
0
%%EOF

197 0 obj
<</C 163/Filter/FlateDecode/I 185/Length 151/O 147/S 94>>stream
hÞb```¢vV3AŠ dž%êŒzŽÅ–¯ K   ™ULT«Ú1Q5}ukGGGGƒFGG#Õ#>(f`dàg¬‘áú¨}Àœ!“‹óÆÄF  †—?O1x7€•°30ínÒŒ@ôHs00íÍa ;•Sn$Óu¨*E€  »!S
endstream
endobj
166 0 obj
<</MarkInfo<</Marked true>>/Metadata 10 0 R/Outlines 14 0 R/PageLayout/OneColumn/Pages 163 0 R/StructTreeRoot 25 0 R/Type/Catalog>>
endobj
167 0 obj
<</Contents 171 0 R/CropBox[0.0 0.0 612.0 792.0]/MediaBox[0.0 0.0 612.0 792.0]/Parent 163 0 R/Resources<</ColorSpace<</CS0 182 0 R/CS1 183 0 R>>/Font<</C2_0 188 0 R/TT0 190 0 R/TT1 192 0 R/TT2 194 0 R/TT3 196 0 R>>>>/Rotate 0/StructParents 0/Type/Page>>
endobj
168 0 obj
<</Filter/FlateDecode/First 121/Length 1269/N 15/Type/ObjStm>>stream

更多信息:

PDF现在可以正确下载...摆弄但从未改变任何东西,总是ctrl +z

最新爆料:

一个空格被添加到文件的顶部,这会破坏图像......但是我不知道如何以编程方式摆脱它。

4

1 回答 1

0

它与 Windows 和 Unix 行尾有关,因为它是基于 unix 的 Web 服务器,\cr\lf适用于\lf应有的地方。因此,图像在开始时使用空格来渲染,这会破坏整个文件。

于 2014-07-22T21:45:51.407 回答