PHP头文件损坏的文件从数据库返回的文件? [英] Corrupted File from PHP Header File returned from database?

查看:106
本文介绍了PHP头文件损坏的文件从数据库返回的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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



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

 函数header_file($ data,$ file_data)
{
$ last_modified = gmdate('D,d MYH:i:s',$ file_data ['unix_last_modified_time'])。GMT;


//如果浏览器询问它是否最新
if(isset($ _ SERVER ['HTTP_IF_MODIFIED_SINCE']))
{
//解析头文件
$ if_modified_since = preg_replace('/;.*$/','',$ _SERVER ['HTTP_IF_MODIFIED_SINCE']);
$ b $ if($ if_modified_since == $ last_modified)
{
//浏览器的缓存仍然是最新的
header(HTTP / 1.0 304 Not Modified );
头(Cache-Control:max-age = 86400,must-revalidate);
出口;



header(Cache-Control:max-age = 86400,must-revalidate);
header(Last-Modified:。$ last_modified);
header(Content-Type:。$ file_data ['file_upload_type']);
//这样可以防止缓存...
//是的,很多头发都输给了这个...
// header(Content-Length:。strlen($ data) );
头(Content-Transfer-Encoding:binary);
$ b $ 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'])。\);

//发送数据到输出
echo $ data;
出口;

$ / code>

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

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

结果:

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



回应:

 请求URL:http://www.example.com/index.php?module = uploads& sub_module = getfile& id = 4982 
请求方法:GET
状态码:200 OK
请求Headersview源
接受: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
连接:keep-alive
Cookie:banner89 = yes; banner90 =是; PHPSESSID = vhlk92ihtcdmtv2q4vhjbmsv54; __utmz = 45276912.1383308583.8.1.utmccn =(直接)| utmcsr =(直接)| utmcmd =(无); __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,像Gecko)Chrome / 30.0.1599.101 Safari / 537.36
查询字符串Parametersview sourceview URL编码
模块:uploads
sub_module:getfile
id:4982
响应Headersview源
Cache-Control:max-age = 86400,必须重新验证
连接:keep-alive
Content-Disposition:attachment;文件名=Masthead-Banner.gif
内容编码:gzip
内容长度:5920
内容类型:image / gif
日期:2013年11月5日:14:03 GMT
过期时间:1981年11月19日星期四08:52:00 GMT
最后修改时间:星期二,2013年11月05日08:27:55 GMT
Pragma:no-cache
服务器:Apache
Vary:接受编码
X-Cache:来自防火墙的MISS。

更多调查:

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

实物:

 %PDF-1.5 
%ÏÓ
165 0 obj
< /线性化1 / L 100758 / O 167 / E 86272 / N 4 / T 100409 / H>>
endobj
$ b 181 0 obj
<< DecodeParms > / 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 b b b b b b b b b b b b b b b b b b b b b endobj
startxref
0
%% EOF

197 0 obj
hÞb```vV3AÇ%êzÅK ULT«Ú1Q5} ukGGGGFGG#Ã#>>(f`dàg¬á¨¨üÆóÆÄF?O1x7° S
endstream
endobj
166 0 obj
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] /父母163 0 R /资源> /字符 endobj
168 0 obj
/ Filter / FlateDecode / First 121 / Length 1269 / N 15 / Type / ObjStm>> stream



损坏:

 %PDF-1.5 b 

165 0 obj
< /线性化1 / L 100758 / O 167 / E 86272 / N 4 / T 100409 / H [498 245]>>
endobj
$ b 181 0 obj
<< DecodeParms > / 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 7
endstream
endobj
startxref
0
%% EOF

197 0 obj
hÞb``vV3AŠ†%êŒzÅ-K™ULT«Ú1Q5}ukGGGGƒFGG#Ã##> ; S
endstream
endobj $ b $; $ s
endstream



$ b 166 0 obj
<< / MarkInfo<< /标记真>> /元数据10 0 R /轮廓14 0 R / PageLayout / OneColumn / Pages 163 0 R / StructTreeRoot 25 0 R /目录>>
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>> /字符>> /旋转0 / StructParents 0 / Type / Page>>
endobj
168 0 obj
/ Filter / FlateDecode / First 121 / Length 1269 / N 15 / Type / ObjStm>> stream



更多信息

PDF正在正确下载... ...摆弄周围,但从来没有改变任何东西,总是 ctrl + z



最新启示: strong>



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

解决方案

它与Windows和Unix Line Endings有关,因为它是一个基于unix的web服务器 \cr \lf 适用于只有 \lf 应该的地方。因此,图像在开始时会以一个空格呈现,这会破坏整个文件。


I have the following function that gets a blob from the database and should return the file to the browser, however it is returning a corrupt file:

$file_data is an array with the returned row from the files table with the blob, the content type, last modified and other such things.

$data is the blob component of the $file_data array.

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;
            }

Before the function is run, the output buffer is cleared with:

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

Results:

File downloads, with the correct filesize however it is corrupted.Related question: https://stackoverflow.com/questions/19768650/zend-caching-of-images-gives-problems-once-the-site-goes-down-for-a-while

Response:

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.

More investigation:

Opening the downloaded file and comparing it with the one on the server reveals that the header and footer of the text is the same but there are a lots of characters that are different:

Real Thing:

%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

Corrupted:

%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

More info:

PDF's are now downloading correctly...fiddled around but never changed anything, always ctrl +z

latest Revelations:

A space is added to the top of files which corrupts the image...However i don't know how to get rid of it programmatically.

解决方案

It has to do with Windows and Unix Line Endings because it is a unix based web server the \cr\lf applies where just the \lf should. Hence the images are being rendered with a space at the beginning which corrupts the entire file.

这篇关于PHP头文件损坏的文件从数据库返回的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆