PHP - 强制文件下载脚本 - 在 IF 语句中不起作用
下面的代码是一个带有 IF 语句调用下载页面的页面。
if ($currentpdt<$updatedpdt)
echo "updatedpdt is greater than currentpdt.";
else
echo "updatedpdt is not greater than currentpdt";
require("dlfile.php");
?>
上面的页面还有其他内容,大部分只是用于定义变量的 SQL 连接。请注意,如果 IF 语句为 false,则需要“dlfile.php”文件。该文件如下
<?php
// local file that should be send to the client
$local_file = 'dl.zip';
// filename that the user gets as default
$download_file = 'your-download-name.zip';
// set the download rate limit (=> 20,5 kb/s)
$download_rate = 20.5;
if(file_exists($local_file) && is_file($local_file)) {
// send headers
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($local_file));
header('Content-Disposition: attachment; filename="'.$download_file.'"');
// flush content
flush();
// open file stream
$file = fopen($local_file, "r");
while(!feof($file)) {
// send the current file part to the browser
print fread($file, round($download_rate * 1024));
// flush the content to the browser
flush();
// sleep one second
sleep(1);
}
// close file stream
fclose($file);}
else {
die('Error: The file '.$local_file.' does not exist!');
}
?>
当我单独运行 dlfile.php 文件时,它工作得很好,并且下载提示按预期显示。然而,当第一个文件调用它时,这就是我得到的。
PKÇ`[email protected] %DÍN%DÍNUxz{]’ÏNÃ0ÆÏÉSX»¬›ª¶š8 PÅ×Á¤©ÛÚ8ÊŸmíÝIX7MœâXŸþì¤ZzE™`:Ä6vÜÀ/·œ}8?(ÝUæ zÔh…G©‚£Ò-¡#Êã£Ò_î¦k9kæˆAxøÝÛÚƒ’XÀ{Lj1!P÷¯Ïœ< éÇ䌱›CÎ8º…™H¡!ü¹DÔbÔŽÑJ´éJÕÍË©äÍ% ,:Îöˆ0xožÊ2awŸ® Û—Q†§ÂæÅÕµ|[rÎÊœ·J÷÷èÿDÎÂÂ4_PÃÊ°µ¦|$)Òp)n•EéÉ*té:¡‹ÊIKc*qõŒ.w>².a$ªoÑŒ˜dGÕú¡~¬ª|@Õ¾~¨ªÕsZM\E'6¿ø+È Î >këeиËu~çt+UÙRÑ‘.~‚u¹g›3?ó_PKÇ`w?ÌmO‹jpreview.phpUT %DÍN%DÍNUxz{VmoÛ6þlýŠ«ÔöKq›]lÙh“¬/ȶ ñVÛ`Ðe3–I¤ì&AþûîHù-ΧmùDw÷<<ÝÝÃSXÎÊ Ž¥)7R%s14³BI0ÜZ!§&ÐüïJh>V2åfÎyöêÖŒ}p„Ív/ˆæ=·°dZ°IÁ äZ-@Ȳ²àÈèh ƒÄ£ñûËÑM´5ÿj·a»8ƒf³¥3& dÜÇxî%«Åmûg@È 2n3#ã>Ä[ÓE«ììê‰P:’Òɘå‡ìh|Âî,”½?ÀØû}=S<5ÄA˜ž‚Ðøæ,›ƒfvQ ȸò– ª²ùmûgØ´E3ž¨oc‘ ·®}‚]ûz&ÊÃV•OU¦ÛLÝ\Zn,äJÂ"ª2P*“jQZ°lÊM rh«KeZ4¬ÇÐì{ÿ ‰ôA£áRóéXó²`¨‡0nýY‡µ[Ñ÷Ãv«Æ´c#ÂcoÎ?¼ÁÅåÕåèò‚,D¢f™@ü¦%Êì¼µ–/(…ª¹å©…[¶du†·háYo'Jã?ÀdV³tŽ`(Ôª’f(‹ˆúѽ··a^Њ۸r¸ÞÛJK¤S²á à"Iš¬Ù†µ2P$D7©D‘AÉ4[`6°Vt61Ù‡úb€JÁ‘ªI¨k5³“󋄃Üëˆ(ð%¹ö|0©cOžF¢=!ç:Î¥ø\ 9ç^‡:Í>>«çÞ°RÝž%WâÖÁ¨U u:|mïôo"I¢ÏÓ’+që`×M0=·U _º>9óàD™ÜöÉ›/Ñ“]ØŽ‰ý°Æ$;AkvÞ³äeê¸/q J¯ZC÷$„ˆƒËTeÜÉ«–-Ñþ"Û‘T~ZÇÿ…Ósu¢‡ƒ 迸øõ|ôõú>Œ~¾‚ëßÞ]}<‡°Ç_^ŸÇñÅèÂ;N£“.Œ4“F+âøò—0€ú'œY[žÅñjµŠV¯#¥§ñèsL'Æ…R†G™ÍÂAÐw=,˜œ&!—ÎÀY6}Ú¼4uçÖµ} cƒ§3…×êŒÕ¡¼LB\º–KÛ±w%¡~JBË¿Yw|PCïÆ);o¾7Ò=/ðÂû¿¸…Q7o~ø±ÓÝ4†ôZVØ‚~Â6Áälך/_õcïÁBÈ9h^$¡±wø0ã܆0Ó<¯-ñ„‘v#œC¬"œ(:ì&xNµªdÖIU¡ôÙwyž÷ð¦×S!qW¿*¿õƒYào*|½œ-Dqw¿s1ÉŽá-~°ÇðKnEŠƒ}ï®EÞqÉ]=Q?.w´_ú›º!®m^øN‚ëër8ØmK}ïῬ–Å.èéµHèß©ú¾;añ–Â7¿z¦Žâš:‹+{üǹŸÿ¦!~¿>ž»Ú}qeñãà?œæ(P^ä‘«k«ºª©b<Ç=»¿ýØÏTß]hÿPKÇ`w?6J@T1 ¤popup.jsUT%DÍNUxPKÇ`w?ÌmO‹j ¤preview.phpUT%DÍNUxPK‰X
我认为浏览器正在尝试将该文件作为网络文档读取,但我不确定。我试图取出 dlfile.php 中的标签,并且还将代码切换
header('Content-Length: '.filesize($local_file));
到
header('Content-Disposition: attachment; filename="'.$download_file.'"');
仍然可以正常执行的代码,但在原始文件调用时生成了相同的奇怪输出。
我尝试将 dlfile.php 中的代码放入原始文件中并获得相同的输出。
怎么了?
The bellow code is a page with an IF statement calling the downloadpage.
if ($currentpdt<$updatedpdt)
echo "updatedpdt is greater than currentpdt.";
else
echo "updatedpdt is not greater than currentpdt";
require("dlfile.php");
?>
The above page has other things on it, mostly just SQL connections to define variables. Notice if the IF statement is false the "dlfile.php" file is required. This file is as follows
<?php
// local file that should be send to the client
$local_file = 'dl.zip';
// filename that the user gets as default
$download_file = 'your-download-name.zip';
// set the download rate limit (=> 20,5 kb/s)
$download_rate = 20.5;
if(file_exists($local_file) && is_file($local_file)) {
// send headers
header('Cache-control: private');
header('Content-Type: application/octet-stream');
header('Content-Length: '.filesize($local_file));
header('Content-Disposition: attachment; filename="'.$download_file.'"');
// flush content
flush();
// open file stream
$file = fopen($local_file, "r");
while(!feof($file)) {
// send the current file part to the browser
print fread($file, round($download_rate * 1024));
// flush the content to the browser
flush();
// sleep one second
sleep(1);
}
// close file stream
fclose($file);}
else {
die('Error: The file '.$local_file.' does not exist!');
}
?>
When I run the dlfile.php file by itself it works just fine and the download prompt appears as it should. However when it is called by the first file this is what I get.
PKÇ`[email protected] %DÍN%DÍNUxz{]’ÏNÃ0ÆÏÉSX»¬›ª¶š8 PÅ×Á¤©ÛÚ8ÊŸmíÝIX7MœâXŸþì¤ZzE™`:Ä6vÜÀ/·œ}8?(ÝUæ zÔh…G©‚£Ò-¡#Êã£Ò_î¦k9kæˆAxøÝÛÚƒ’XÀ{Lj1!P÷¯Ïœ< éÇ䌱›CÎ8º…™H¡!ü¹DÔbÔŽÑJ´éJÕÍË©äÍ% ,:Îöˆ0xožÊ2awŸ® Û—Q†§ÂæÅÕµ|[rÎÊœ·J÷÷èÿDÎÂÂ4_PÃÊ°µ¦|$)Òp)n•EéÉ*té:¡‹ÊIKc*qõŒ.w>².a$ªoÑŒ˜dGÕú¡~¬ª|@Õ¾~¨ªÕsZM\E'6¿ø+È Î >këeиËu~çt+UÙRÑ‘.~‚u¹g›3?ó_PKÇ`w?ÌmO‹jpreview.phpUT %DÍN%DÍNUxz{VmoÛ6þlýŠ«ÔöKq›]lÙh“¬/ȶ ñVÛ`Ðe3–I¤ì&AþûîHù-ΧmùDw÷<<ÝÝÃSXÎÊ Ž¥)7R%s14³BI0ÜZ!§&ÐüïJh>V2åfÎyöêÖŒ}p„Ív/ˆæ=·°dZ°IÁ äZ-@Ȳ²àÈèh ƒÄ£ñûËÑM´5ÿj·a»8ƒf³¥3& dÜÇxî%«Åmûg@È 2n3#ã>Ä[ÓE«ììê‰P:’Òɘå‡ìh|Âî,”½?ÀØû}=S<5ÄA˜ž‚Ðøæ,›ƒfvQ ȸò– ª²ùmûgØ´E3ž¨oc‘ ·®}‚]ûz&ÊÃV•OU¦ÛLÝ\Zn,äJÂ"ª2P*“jQZ°lÊM rh«KeZ4¬ÇÐì{ÿ ‰ôA£áRóéXó²`¨‡0nýY‡µ[Ñ÷Ãv«Æ´c#ÂcoÎ?¼ÁÅåÕåèò‚,D¢f™@ü¦%Êì¼µ–/(…ª¹å©…[¶du†·háYo'Jã?ÀdV³tŽ`(Ôª’f(‹ˆúѽ··a^Њ۸r¸ÞÛJK¤S²á à"Iš¬Ù†µ2P$D7©D‘AÉ4[`6°Vt61Ù‡úb€JÁ‘ªI¨k5³“󋄃Üëˆ(ð%¹ö|0©cOžF¢=!ç:Î¥ø\ 9ç^‡:Í>>«çÞ°RÝž%WâÖÁ¨U u:|mïôo"I¢ÏÓ’+që`×M0=·U _º>9óàD™ÜöÉ›/Ñ“]ØŽ‰ý°Æ$;AkvÞ³äeê¸/q J¯ZC÷$„ˆƒËTeÜÉ«–-Ñþ"Û‘T~ZÇÿ…Ósu¢‡ƒ 迸øõ|ôõú>Œ~¾‚ëßÞ]}<‡°Ç_^ŸÇñÅèÂ;N£“.Œ4“F+âøò—0€ú'œY[žÅñjµŠV¯#¥§ñèsL'Æ…R†G™ÍÂAÐw=,˜œ&!—ÎÀY6}Ú¼4uçÖµ} cƒ§3…×êŒÕ¡¼LB\º–KÛ±w%¡~JBË¿Yw|PCïÆ);o¾7Ò=/ðÂû¿¸…Q7o~ø±ÓÝ4†ôZVØ‚~Â6Áälך/_õcïÁBÈ9h^$¡±wø0ã܆0Ó<¯-ñ„‘v#œC¬"œ(:ì&xNµªdÖIU¡ôÙwyž÷ð¦×S!qW¿*¿õƒYào*|½œ-Dqw¿s1ÉŽá-~°ÇðKnEŠƒ}ï®EÞqÉ]=Q?.w´_ú›º!®m^øN‚ëër8ØmK}ïῬ–Å.èéµHèß©ú¾;añ–Â7¿z¦Žâš:‹+{üǹŸÿ¦!~¿>ž»Ú}qeñãà?œæ(P^ä‘«k«ºª©b<Ç=»¿ýØÏTß]hÿPKÇ`w?6J@T1 ¤popup.jsUT%DÍNUxPKÇ`w?ÌmO‹j ¤preview.phpUT%DÍNUxPK‰X
I think the browser is trying to read the file as a web document but I am not sure. I have attempted to take out the tags in dlfile.php and I also switch the
header('Content-Length: '.filesize($local_file));
to
header('Content-Disposition: attachment; filename="'.$download_file.'"');
the code still executed fine by itself but generated the same weird output when called for by the original file.
I have tried to just put the code from dlfile.php into the original file and got the same output.
What's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
事实上并非如此。如果没有大括号,您的代码将被解释为:
尝试使用大括号来提高可读性并解决此问题:
此外,如果您从第一个文件中回显某些内容,则第二个文件将无法设置标头,并且将输出文件的原始内容。因此,您需要从第一个文件中删除回显,以便仅输出 zip 的内容。
Actually it's not. Without braces your code is interpreted as:
Try using braces to increase readability and fix this problem:
Also, if you echo something from you first file then the second file will not be able to set headers and the file's raw contents will be outputted. So you'll need to remove the echo's from your first file so that only the zip's contents gets outputted.
您遇到了悬空的 else 问题,如果不使用括号,则 else 块中仅考虑紧随其后的行。
http://en.wikipedia.org/wiki/Dangling_else
You have the Dangling else problem, if you don't use brackets, only the line immediately followed is considered in the else block.
http://en.wikipedia.org/wiki/Dangling_else
除了其他人正确评论的语法问题之外,调用文件具有
echo()
调用,并且它通过require()
包含的文件具有header( )
调用。但是,如果header()
调用遵循页面的任何输出,则它们将不起作用。因此,您需要摆脱这些echo()
调用。这可能是您得到的古怪输出的来源。Aside from the syntax problem that others have correctly commented on, the calling file has
echo()
calls and the file it is including viarequire()
hasheader()
calls. Butheader()
calls will not work if they follow any output to the page. So you need to get rid of thoseecho()
calls. That may be the source of the wacky output that you're getting.嗯……回声就是问题所在。
尝试在代码开头添加这两行,您会看到问题:
Well... the echos are the problems.
Try adding these two lines at the start of your code, you'll see the problem: