3

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.

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.

Here's my code:

$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();

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Ž•&lt;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?

4

1 回答 1

2

ob_clean(); and flush();根据 PHP 手册关于该主题的说明,在函数之前添加函数readfile();可能是值得使用的。

readfile() http://php.net/manual/en/function.readfile.php

ob_clean() http://php.net/manual/en/function.ob-clean.php

冲洗() http://php.net/manual/en/function.ob-flush.php

您发布的代码中不存在这些功能。

于 2013-07-26T14:52:26.270 回答