文件有时下载为.PHP而不是.PDF? [英] Files sometimes download as .PHP instead of .PDF?
问题描述
我正在使用一个脚本,允许用户下载在线存储的文件。它似乎与图像文件一起使用,但是使用PDF浏览器有时会下载.PHP文件而不是.PDF文件。
I'm working on a script that allows users to download files stored online. It always seems to work with image files, but with PDF's the browser sometimes downloads .PHP files instead of .PDF's.
我没有使用任何其他文件类型测试脚本,所以我不知道问题是否限于PDF,或者是否会发生其他类型的好。
I haven't tested the script with any other file types, so I don't know if the problem is limited to PDF's or if it would happen for other types as well.
这是我的代码:
$mimeTypes = array(
/* images */
'png' => 'image/png', 'jpeg' => 'image/jpeg', 'jpg' => 'image/jpeg', 'gif' => 'image/gif', 'bmp' => 'image/bmp',
/* spreadsheets */
'xl' => 'application/vnd.ms-excel', 'xls' => 'application/vnd.ms-excel', 'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'csv' => 'text/csv', 'sdc' => 'application/vnd.stardivision.calc', 'odc' => 'application/vnd.oasis.opendocument.chart',
/* text files */
'doc' => 'application/msword', 'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'odt' => 'application/vnd.oasis.opendocument.text', 'rtf' => 'application/rtf', 'rtx' => 'text/richtext',
/* PDF */
'pdf' => 'application/pdf'
);
$file = __DIR__ . '/files/' . $row['disk_name'];
header('Content-Description: File Transfer');
header('Content-Type: ' . $mimeTypes[$row['ext']]);
header('Content-Disposition: attachment; filename="'. $row['name'] .'"');
header('Content-Transfer-Encoding: binary');
header('Connection: Keep-Alive');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
die();
下载的.PHP文件比相应的PDF文件大1KB。在记事本中,下载为.PHP的PDF文件中的额外内容如下所示:
The .PHP files that are downloaded are 1KB larger than the corresponding PDF files. In notepad, the extra content in a PDF file downloaded as .PHP looks like this:
Û¼T"gS_åœx¨fꙸx0]Ï~0î…[#X7beú
Œ¾){(‹Ã ‰ždêò¤ž
^`ô}\ȯDdìè¬#¸A,.ÛàÇ3ðuúÑÅIôo GP‘pð.bWædÎVª8¨‹ÃÚï~"½1ÚùYGrƒ^º9YçãžÁa0]žÿ{VP\§•ÑúÄ›\²/Óa4§ R Â×ïYGrøå3¨$Ë Køpf%„ HÐá@~Tyw«ðŠfPý—@òwæà– Ác8rI:2ëÅ*P5„*‚øïÖ`»´æí£³Èx€–T$W³d´ HÏy»…G˜ÊÃ?Ž=TÐð+PÊÃc6’q
¥üQ˜Q¦GØšOQò‹¹t‡€öGz‚‘«mØÌ&Æ5kPnd Ôs˜ù7Ê¢ø=à¦Uïºd"Âœ¸ÐîÌóÏlnŒVšÊ¤P[¾ò¸ª‹„mÊO/
‹xï$ Ƶ¤%0´7M?ÒÙdaã1ÓKùaPÌZó†A¬møY¨¿D›K²Ji;w €9ö>*®h³³†‚wRy¶=s+U\ñd¤™ýØ·ÇΗú®Q–kÈ(˜´jñWQd©¢à¦MÌw½Ÿt°Z±Þa¾co{Ö`ȳŸô¯òje©ÒPióBL$"ÓP/&´ÆÞµš9h–)lŸk )
À„uÚº'Ûˆ"Ô_4~‹}]?óâùg3 Ô¨?ËL¡ê*¢‰"çS) M³‘*LâGG…®sœùw;¤N`Åx½cëu 'oJæeÆ_M½ËÒXÈ…³„{ÈŃ)õ³P´œ»SI™P,éH@¸’ÄÌ^ÓÙb§[id˜_^çî>zflø€aÞ‹€% G‘,=ùI¦ÕaˆVEÃvoûl•>`ÂÆJ^V›ƒ
:"ˆi„ÄÐ’@€Ð—ìRy¯ '¡šË¢›Øƒ
Æ[&ÉF›’_+Õ¢$ÜÜMãºÏ€u},w†¼BœÄA`R¤dYm)Œ$¤<},<³Ó_¥æËÒP|&Tj; •ê¥U0h|&Õ}©¸t‹s *1žõŽ2„E'…ÛnäYjë— Tg¡’v•[ ¥ì¤îK¨ä–t$ üñ\؉¸X€R+e^ªx;£ÌË&ä´R®ðúpR_Åíf ^,vžÒJcAŽ•<O€LR1%ªP¡Ãê–x]º¬#Îs?¨üVŽ*jæ ¤Úbª£ê£«â7îm]û$ ÇUfGÄo£ÒÛþO#ƒÞ©?t±ª·PN¹¡8¨i°<ôyZ_lGÞJÕ\Y{®Hõ X~
¸Å‘"H; «™
a™Žl%BÅׄ]Œ‹ešµR…JÏeÕÒ(gH©,ÓÚaIR/¤¬Ÿ#? J ¸’„"ã.žÚ#§j¥Š³çŠtâÅ°^)¹Ï(Pm°¢¨–ÕÒ‹ˆk)ÂÚý£Ò5 $W¬WPa&/çG1ÈDaÕû²Žâ:¥§š™"2HŸˆUë«WŒRF°"²yݳYEÂÁoØŠ?@‘dŠ-HVÑîÝ?jÆr)¨º7¥Bd–•u$¼&Eê®eùµRÂf,UË Û«‚•jõYšU²±Ú!ò#¼–aÐÍU°ÚsVTö
ìÄ^úª•
Tصr‡gPùÅ™0©S"ûË‘.œÅå• ³‘„ôjÞ6ËÂOçÌAj?j÷,¬ÎpRŸx}Äd$ÌpÓxÇnÚŽ3yä/rž©öU™ö¢‘ú²ÇGÁ£È¡/L<賎Drþy·³Ú¨•*nîªS±µN+¶IÓª5ô®üÕ¶fâ•×|+ëvØ×J·¶>ž²ÞÅ&4k>HTTP/1.1 200 OK
Date: Fri, 26 Jul 2013 03:24:16 GMT
Server: Apache/2.4.2 (Win64) PHP/5.4.3
X-Powered-By: PHP/5.4.3
Expires: 0
Cache-Control: must-revalidate, post-check=0, pre-check=0
Pragma: public
Content-Description: File Transfer
Content-Disposition: attachment; filename="bst-bmp180-ds000-08.pdf"
Content-Transfer-Encoding: binary
Connection: Keep-Alive, Keep-Alive
Content-Length: 752201
Keep-Alive: timeout=5, max=99
Content-Type: application/pdf
我做错了什么导致这种情况发生?
What am I doing wrong that is causing this to happen?
推荐答案
添加 ob_clean();
和flush();
函数之前的 readfile();
函数,可能是值得的使用,根据PHP手册中关于该主题的说明。
Adding ob_clean();
and flush();
functions before the readfile();
function, could be something worth using, as per what the PHP manual states on the subject.
readfile()
http://php.net/manual/en/function.readfile.php
readfile() http://php.net/manual/en/function.readfile.php
ob_clean() http://php.net/manual/en/function.ob- clean.php
ob_clean() http://php.net/manual/en/function.ob-clean.php
flush()
http://php.net/manual/en/function.ob-flu sh.php
这些功能不在您发布的代码中。
These functions are not present in your posted code.
这篇关于文件有时下载为.PHP而不是.PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!