从数据库返回的PHP头文件中损坏的文件


Corrupted File from PHP Header File returned from database?

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

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

$data是$file_data数组的blob组件。

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-a-while

响应:

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

最新启示:

文件顶部会添加一个空格,从而损坏图像。。。然而,我不知道如何通过编程来消除它。

它与Windows和Unix Line Endings有关,因为它是一个基于Unix的web服务器,'cr'lf应用于'lf应该应用的位置。因此,图像的渲染在开始时有一个空间,该空间会破坏整个文件。