Zend/PHP:向MySQL的BLOB字段上载文件/从中下载文件时出现问题 [英] Zend/PHP: Problem uploading/downloading file to/from MySQL's BLOB field

查看:78
本文介绍了Zend/PHP:向MySQL的BLOB字段上载文件/从中下载文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在像这样上载文件(现在为pdf):(它正在mysql的blob字段中上载文件的内容)

$organizationModel = new Model_Organization_Object( organizationId );
$myFile = file_get_contents( '../path/to/my/file/filename.ext' );
$organizationModel->setOrganizationProfile( $myFile );
$organizationModel->save();

现在,我想从数据库中获取该文件并下载.我在控制器的操作中这样做:(我在这里查看pdf文件的内容,因此下面将其硬编码.但是将来我想从blob字段下载任何文件)

$organizationModel = new Model_Organization_Object( $organizationId );
$content = $organizationModel->getOrganizationProfile();

header('Content-Type: application/octet-stream');
header("Content-Length: " . strlen($content) );
header('Content-Disposition: attachment; filename=orgProfile.pdf');

$this->view->organizationProfile = $content;

现在在查看文件中,我正在这样做:

echo $this->organizationProfile; 

但是,在上述下载过程中,以bugbug打印(回显)文件的内容,而不下载原始格式的文件.我在萤火虫中的回声输出是这样的:

%PDF-1.3
%����
84 0 obj
<< 
/Linearized 1 
/O 86 
/H [ 541 212 ] 
/L 958398 
/E 11238 
/N 27 
/T 956600 
>> 
endobj
                                                        xref
84 7 
0000000016 00000 n
0000000486 00000 n
0000000753 00000 n
0000000982 00000 n
0000001102 00000 n
0000000541 00000 n
0000000732 00000 n
trailer
<<
/Size 91
/Info 83 0 R 
/Root 85 0 R 
/Prev 956590 
/ID[<0a8d7035bf08791da591e8cae39b8c49><0a8d7035bf08791da591e8cae39b8c49>]
>>
startxref
0
%%EOF

85 0 obj
<< 
/Type /Catalog 
/Pages 82 0 R 
>> 
endobj
89 0 obj
<< /S 151 /Filter /FlateDecode /Length 90 0 R >> 
stream
H�b```f``�e`b`�f`@\0�.����\\I~aV$�Xƈ�dǪ����bA�Az�lv1o#�{-����1+�ʪG�����N`�b�
>�-��
\0\0D40
endstream
endobj
90 0 obj
106 
endobj
86 0 obj
<< 
/Type /Page 
/Contents 87 0 R 
/Parent 79 0 R 
/Resources << /XObject << /img0 88 0 R >> /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ] >> 
/MediaBox [ 0 0 612 792 ] 
/CropBox [ 0 0 612 792 ] 
/Rotate 0 
>> 
endobj�@.\0���
endstream
endobj
88 0 obj
<< /Filter /FlateDecode \0\0\0\0\0\0\0\0\0\\
\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\\
\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\\
\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\\
\0\0\0\0\0\0\0\0\0\0\\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\\
\0\0\0\0\0\0\0\0|n�FT*,�
��
�j#Q��uT~r:}\\�_؛�ҵ��v��ϭ8Q�&� ���T�S�I\"�(>Y�ܾ����}H��aj�3��u�h�T�X�Z�-~��c\'P�^��d�ם�ߔ?����]_�ڿ�z��O]�q�����7寋�|�mN%�����̖T�����o�ߓ�sUzT���m�v8ͯq�
��e]�wS���C~Ta���.��[%!������2x]n~�7�Ϫ����6.����K��;c
}����r�)V��
u���*�7�$c\\���m�~���r��)U{�λ�廳˺��ԟ�R��
��D1L_����WUog�>��/������ߦ��~�%���M���}\'�� ;���y��K`�����O�,�߫����<�,0���;3    #��m�v���aZ=�N�u�J`��dwnm;��.Ѣ�k�n�K1-M�7����H&��ʨ���s�C?��
�}Z�1����c�(0�q�_1��7�%���G7U/��h9I������S�Q��4nc�Lq��H6��;�꺒c(/O��2�٫�-�*_����%�I؏/?�I�o����ô�k��<����q��\'��]v�\"��+�˗ݯ�,��ɏ�qxgk�\\\\�6���7��Y���.G��ҽY��8��.��*���M_��J�hu1����z��W�o_��F�/���s�:�Y~��>0�g\\E�l�K5e���&L�/����k$����{ں:\\>�̥Fs?-��l�>c�亪o���Λ�9�V+�2;��}q�4
�zS�|u�A`dK���n~�sΛ��K�hiY�j��#p���S�M\\���0P2䗶\\*�m+?L5Er����[W�>9|�̑�iY�u�j�M�_���n&��7O�f��s��z`.`�,W��#�l��n���s�՛\'�����=��&#�z�M7_����s���x��y�
��u�p�G���0ͨe�G���۸8]{�䓷N�1}��}~Q�[)�XF��_��*?  p7iQ����M�(�l���������׏��f��6����*��Ų;@~\\k�i��w_��*�#�Ւ�^�j�\\�L��/�}�Y�[��V��t~�w�n��a���m�O�(.�n;��ji:��W�ZnQ[9�n=�^��sE9��;�.��u3\"ږ��<�Ļ��y8�<H���g��u��\\�q��Ȥ71p�U��}ם��f`�Y��m3b*C�t{�SX��7m<��6��8K��[Qs��&_��(M��:�Z���W��Հ��W寚
��4d��4�Aڔ�ɪ�lw�e�d�>�
�pCV��h�ŜS�Z�T��4�NӴ,�� �8=-�%ߜ��4�p�a��~��R눥L芈�=J��j}��ʺ��,�(�x����
�]��l�)L��� I8eG#r�dC��;�͹/C���l���rm�ɽ��͆��e�6�M��fP�4�r��)�!�\\sڹ�?{��!cN��h�֗>�� ��o>��m�dO=&<ɻ�P��xΔ=�͌CC?�M��W[ϟ�v<���S14�����\\C�Z
    ��g��ݡq:�ǔ�C�k�vc�K�;��\"Y͙t�r]��G�z����w���rӹ����ަ0������e�:��/f�*^�W�Q8WsN��9}*ۥ|��~x)�N�=6J�l����M�b��Ƿ���M45�C�k]��r�uߍ�����r
]

firebug中的响应标头:

Date    Thu, 15 Apr 2010 06:21:03 GMT
Server  Apache/2.2.11 (Ubuntu) PHP/5.2.6-3ubuntu4.2 with Suhosin-Patch
X-Powered-By    PHP/5.2.6-3ubuntu4.2
Expires Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma  no-cache
Content-Length  65535
Content-Disposition attachment; filename=evidence.pdf
Keep-Alive  timeout=15, max=71
Connection  Keep-Alive
Content-Type    application/octet-stream

有人可以帮忙下载文件的方式吗,或者我在上传过程中做错了什么. setOrganizationProfile()和getOrganizationProfile()函数是由我们的团队创建的,在将数据存储到数据库中或从数据库中获取数据时,它们工作正常.

谢谢

解决方案

好吧,我认为您的问题是您正在使用AJAX下载文件.

无法使用AJAX(或更确切地说,是JS)以正常方式下载文件.您知道,漏洞利用漏洞太大(我认为是这样)-无论如何,这种可能性不大.

尽管有解决方法.其中之一是在页面上动态创建隐藏的iframe,然后将其位置更改为下载脚本.然后,您将不会使用JS进行下载,而是使用普通的浏览器功能.

此页面上还介绍了另一种方法:

http://www.filamentgroup.com/lab/jquery_plugin_for_requesting_ajax_like_file_downloads/

我会为您提供帮助.

I am uploading file(pdf for now) like this: (It is uploading content of file in blob field of mysql)

$organizationModel = new Model_Organization_Object( organizationId );
$myFile = file_get_contents( '../path/to/my/file/filename.ext' );
$organizationModel->setOrganizationProfile( $myFile );
$organizationModel->save();

Now I want to get that file from database and want to download. I doing this in controller's action: (I am aspecting pdf file here therefore it is hardcoded below. But in future I want to download any file from blob field)

$organizationModel = new Model_Organization_Object( $organizationId );
$content = $organizationModel->getOrganizationProfile();

header('Content-Type: application/octet-stream');
header("Content-Length: " . strlen($content) );
header('Content-Disposition: attachment; filename=orgProfile.pdf');

$this->view->organizationProfile = $content;

Now in view file I am doing this:

echo $this->organizationProfile; 

But above download process print(echo) the content of file in firbug and does not download file in orignal format. My echo output in firebug is like this:

%PDF-1.3
%����
84 0 obj
<< 
/Linearized 1 
/O 86 
/H [ 541 212 ] 
/L 958398 
/E 11238 
/N 27 
/T 956600 
>> 
endobj
                                                        xref
84 7 
0000000016 00000 n
0000000486 00000 n
0000000753 00000 n
0000000982 00000 n
0000001102 00000 n
0000000541 00000 n
0000000732 00000 n
trailer
<<
/Size 91
/Info 83 0 R 
/Root 85 0 R 
/Prev 956590 
/ID[<0a8d7035bf08791da591e8cae39b8c49><0a8d7035bf08791da591e8cae39b8c49>]
>>
startxref
0
%%EOF

85 0 obj
<< 
/Type /Catalog 
/Pages 82 0 R 
>> 
endobj
89 0 obj
<< /S 151 /Filter /FlateDecode /Length 90 0 R >> 
stream
H�b```f``�e`b`�f`@\0�.����\\I~aV$�Xƈ�dǪ����bA�Az�lv1o#�{-����1+�ʪG�����N`�b�
>�-��
\0\0D40
endstream
endobj
90 0 obj
106 
endobj
86 0 obj
<< 
/Type /Page 
/Contents 87 0 R 
/Parent 79 0 R 
/Resources << /XObject << /img0 88 0 R >> /ProcSet [ /PDF /Text /ImageB /ImageC /ImageI ] >> 
/MediaBox [ 0 0 612 792 ] 
/CropBox [ 0 0 612 792 ] 
/Rotate 0 
>> 
endobj�@.\0���
endstream
endobj
88 0 obj
<< /Filter /FlateDecode \0\0\0\0\0\0\0\0\0\\
\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\\
\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\\
\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\\
\0\0\0\0\0\0\0\0\0\0\\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\\
\0\0\0\0\0\0\0\0|n�FT*,�
��
�j#Q��uT~r:}\\�_؛�ҵ��v��ϭ8Q�&� ���T�S�I\"�(>Y�ܾ����}H��aj�3��u�h�T�X�Z�-~��c\'P�^��d�ם�ߔ?����]_�ڿ�z��O]�q�����7寋�|�mN%�����̖T�����o�ߓ�sUzT���m�v8ͯq�
��e]�wS���C~Ta���.��[%!������2x]n~�7�Ϫ����6.����K��;c
}����r�)V��
u���*�7�$c\\���m�~���r��)U{�λ�廳˺��ԟ�R��
��D1L_����WUog�>��/������ߦ��~�%���M���}\'�� ;���y��K`�����O�,�߫����<�,0���;3    #��m�v���aZ=�N�u�J`��dwnm;��.Ѣ�k�n�K1-M�7����H&��ʨ���s�C?��
�}Z�1����c�(0�q�_1��7�%���G7U/��h9I������S�Q��4nc�Lq��H6��;�꺒c(/O��2�٫�-�*_����%�I؏/?�I�o����ô�k��<����q��\'��]v�\"��+�˗ݯ�,��ɏ�qxgk�\\\\�6���7��Y���.G��ҽY��8��.��*���M_��J�hu1����z��W�o_��F�/���s�:�Y~��>0�g\\E�l�K5e���&L�/����k$����{ں:\\>�̥Fs?-��l�>c�亪o���Λ�9�V+�2;��}q�4
�zS�|u�A`dK���n~�sΛ��K�hiY�j��#p���S�M\\���0P2䗶\\*�m+?L5Er����[W�>9|�̑�iY�u�j�M�_���n&��7O�f��s��z`.`�,W��#�l��n���s�՛\'�����=��&#�z�M7_����s���x��y�
��u�p�G���0ͨe�G���۸8]{�䓷N�1}��}~Q�[)�XF��_��*?  p7iQ����M�(�l���������׏��f��6����*��Ų;@~\\k�i��w_��*�#�Ւ�^�j�\\�L��/�}�Y�[��V��t~�w�n��a���m�O�(.�n;��ji:��W�ZnQ[9�n=�^��sE9��;�.��u3\"ږ��<�Ļ��y8�<H���g��u��\\�q��Ȥ71p�U��}ם��f`�Y��m3b*C�t{�SX��7m<��6��8K��[Qs��&_��(M��:�Z���W��Հ��W寚
��4d��4�Aڔ�ɪ�lw�e�d�>�
�pCV��h�ŜS�Z�T��4�NӴ,�� �8=-�%ߜ��4�p�a��~��R눥L芈�=J��j}��ʺ��,�(�x����
�]��l�)L��� I8eG#r�dC��;�͹/C���l���rm�ɽ��͆��e�6�M��fP�4�r��)�!�\\sڹ�?{��!cN��h�֗>�� ��o>��m�dO=&<ɻ�P��xΔ=�͌CC?�M��W[ϟ�v<���S14�����\\C�Z
    ��g��ݡq:�ǔ�C�k�vc�K�;��\"Y͙t�r]��G�z����w���rӹ����ަ0������e�:��/f�*^�W�Q8WsN��9}*ۥ|��~x)�N�=6J�l����M�b��Ƿ���M45�C�k]��r�uߍ�����r
]

reponse headers in firebug:

Date    Thu, 15 Apr 2010 06:21:03 GMT
Server  Apache/2.2.11 (Ubuntu) PHP/5.2.6-3ubuntu4.2 with Suhosin-Patch
X-Powered-By    PHP/5.2.6-3ubuntu4.2
Expires Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma  no-cache
Content-Length  65535
Content-Disposition attachment; filename=evidence.pdf
Keep-Alive  timeout=15, max=71
Connection  Keep-Alive
Content-Type    application/octet-stream

Can someone help that how to download file or I am doing something wrong with uploading process. setOrganizationProfile() and getOrganizationProfile() function are created by our team which are working fine while storing/getting data to/from database.

Thanks

解决方案

Ok, I think your problem is that you're using AJAX to download file.

It's not possible to download file using AJAX (or rather - JS) in a normal way. You know, there's too big hole for exploits to allow that (I think so) - anyway there's no such possibility.

There are workarounds though. One of them is creating hidden iframe dynamically on the page and then changing its location to your download script. Then you're not using JS to download, but plain browser capabilities.

Another way is described on this page:

http://www.filamentgroup.com/lab/jquery_plugin_for_requesting_ajax_like_file_downloads/

Mybe it'll be a help for you.

这篇关于Zend/PHP:向MySQL的BLOB字段上载文件/从中下载文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆