PHP 文件下载除了 .pdf 文件之外的任何文件都会产生垃圾
我遇到的问题是,唯一能够从服务器正确下载的文件类型是“.pdf”。其他文件类型的其他文件存在于同一位置,上传完全正常,具有正确的权限,并且当我将它们发送到本地计算机时加载得很好。我无法显示所涉及的完整代码,因为这是付费软件,但老实说,问题似乎不应该出在代码本身......
我尝试用类似的东西重写下面的第一个函数对此: http://www.finalwebsites.com/forums/topic/php- file-download 然后简单地向其提供正确的值以使其从服务器中选择特定文件。无论哪种方式下载的文件都是相同的。它们会显示为正确的文件类型,具有正确的名称,甚至正确的大小,但它们不会以垃圾值之外的任何形式打开(除非是 PDF)。它是一个运行 freeBSD 8.2-RELEASE 的 apache 服务器。
不管怎样,这是一个类的一部分,包含在它后面的代码中:
1600 function download_file($fileDir,$fileName,$instance_name)
1601 {
1602 $fileString=$fileDir.'/'.$fileName; // combine the path and file
1603 // translate file name properly for Internet Explorer.
1604 if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
1605 {
1606 $instance_name = preg_replace('/\./', '%2e', $instance_name, substr_count($instance_name, '.') - 1);
1607 }
1608 // make sure the file exists before sending headers
1609
1610 if(!$fdl=@fopen($fileString,'r'))
1611 {
1612 die("Cannot Open File!");
1613 }
1614 else
1615 {
1616 header("Cache-Control: ");// leave blank to avoid IE errors
1617 header("Pragma: ");// leave blank to avoid IE errors
1618 header("Content-type: application/octet-stream");
1619 header("Content-Disposition: attachment; filename=\"".$instance_name."\"");
1620 header("Content-length:".(string)(filesize($fileString)));
1621 sleep(1);
1622 fpassthru($fdl);
1623 }
1624 }
现在是使用它的代码:
63 function download_att($supp_obj,$a_predefined) 64 {
65
66 $get_vars=$a_predefined['get'];
67 $att_id=$get_vars['att_id'];
68 $article_attachment=$supp_obj->prefix_table("article_attachment");
69 $sql="select * from $article_attachment where att_id=$att_id";
70 $a_attach=$supp_obj->get_a_line($sql);
71
72 $instance_name=$a_attach['attachment_name'];
73 $att_name_arr=split("\.",$instance_name);
74
75 list($n,$ext)=split("\.", $instance_name);
76
77
78 $fileName="kb_".$a_attach['article_id']."_".$att_id.".".$ext;
79 $fileDir="plugins/knowledgebase/attachments";
80 $supp_obj->download_file($fileDir,$fileName,$instance_name);
81 }
82 //----------------------------------------------------------------------------------------------------------------------
83
84 if($a_predefined['get']['act']=="artattach")
85 {
86 download_att($supp_obj,$a_predefined);
87 exit;
88 }
I am having an issue in which the only filetype capable of being downloaded correctly from the server is ".pdf". Other files of other file types exist in the same location, upload perfectly fine, have the correct permissions, and load just fine when I scp them off to my local machine. I can't show the entirety of the code involved since this is paid software, but honestly it seems that the problem shouldn't even lie in the code itself...
I've tried re-writing the first function below with something similar to this: http://www.finalwebsites.com/forums/topic/php-file-download and then simply feeding it the correct values to make it pick specific files from the server. The files downloaded the same either way. They would show up as the correct file type with the correct name and even the correct size, but they would not open as anything but garbage values (unless a PDF). It is an apache server running freeBSD 8.2-RELEASE.
Either way, this is part of a class that is included by the code following it:
1600 function download_file($fileDir,$fileName,$instance_name)
1601 {
1602 $fileString=$fileDir.'/'.$fileName; // combine the path and file
1603 // translate file name properly for Internet Explorer.
1604 if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
1605 {
1606 $instance_name = preg_replace('/\./', '%2e', $instance_name, substr_count($instance_name, '.') - 1);
1607 }
1608 // make sure the file exists before sending headers
1609
1610 if(!$fdl=@fopen($fileString,'r'))
1611 {
1612 die("Cannot Open File!");
1613 }
1614 else
1615 {
1616 header("Cache-Control: ");// leave blank to avoid IE errors
1617 header("Pragma: ");// leave blank to avoid IE errors
1618 header("Content-type: application/octet-stream");
1619 header("Content-Disposition: attachment; filename=\"".$instance_name."\"");
1620 header("Content-length:".(string)(filesize($fileString)));
1621 sleep(1);
1622 fpassthru($fdl);
1623 }
1624 }
And now the code that uses it:
63 function download_att($supp_obj,$a_predefined) 64 {
65
66 $get_vars=$a_predefined['get'];
67 $att_id=$get_vars['att_id'];
68 $article_attachment=$supp_obj->prefix_table("article_attachment");
69 $sql="select * from $article_attachment where att_id=$att_id";
70 $a_attach=$supp_obj->get_a_line($sql);
71
72 $instance_name=$a_attach['attachment_name'];
73 $att_name_arr=split("\.",$instance_name);
74
75 list($n,$ext)=split("\.", $instance_name);
76
77
78 $fileName="kb_".$a_attach['article_id']."_".$att_id.".".$ext;
79 $fileDir="plugins/knowledgebase/attachments";
80 $supp_obj->download_file($fileDir,$fileName,$instance_name);
81 }
82 //----------------------------------------------------------------------------------------------------------------------
83
84 if($a_predefined['get']['act']=="artattach")
85 {
86 download_att($supp_obj,$a_predefined);
87 exit;
88 }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
其他文件是什么类型?它们是否应该像您一样作为八位字节流提供服务?:
例如,如果您提供带有该标头的文本文件,则可能无法保证浏览器如何解释该文件(或者更准确地说,该响应) 。
What types are the other files? Are they supposed to be served as octet-streams as you're doing?:
If, for example, you serve a text file with that header then how the browser interprets that file (or, more accurately, that response) may not be guaranteed.
其他文件是否可能不是 application/octet-stream 类型?
确保发送正确的 mime 类型标头。
Are the other files perhaps not of type application/octet-stream?
Make sure you send the correct mime-type header.
确保下载文件中没有空行或除 PHP 之外的任何内容,因为这会将 mimetype 默认为
Content-type: text/html
并向您下载的文件添加其他垃圾数据。Make sure there are no blank lines or anything but PHP in your download file, as this will default the mimetype to
Content-type: text/html
and add additional garbage data to the file you download.