Files sometimes download as .PHP instead of .PDF?(文件有时下载为 .PHP 而不是 .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?
推荐答案
在 readfile() 之前添加
函数,根据 PHP 手册中关于该主题的说明,可能值得使用.ob_clean();
and flush();
函数;
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.
读取文件()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
冲洗()http://php.net/manual/en/function.ob-flush.php
您发布的代码中没有这些函数.
These functions are not present in your posted code.
这篇关于文件有时下载为 .PHP 而不是 .PDF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:文件有时下载为 .PHP 而不是 .PDF?
基础教程推荐
- 使用 PDO 转义列名 2021-01-01
- HTTP 与 FTP 上传 2021-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01