开发者

Zend/PHP: Problem uploading/downloading file to/from MySQL's BLOB field

开发者 https://www.devze.com 2022-12-26 18:08 出处:网络
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 );

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/o开发者_C百科ctet-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.


You want the Content-type to be application/pdf not application/octet-stream. That will render the pdf file rather than display the 'source' of it.

Cheers

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号