0

When I am downloading an image it's displaying these characters on screen.

�M\cIDATx��\i��v�U�ݳ��x���6`8�Y�&@��#D �����HDʟ'�~�z;a���D,�c������ƌ���ٺ�*�w�=շjz^z�ـ���UwWWݺ�;�|g��&H���o�Gy���~s��K۲e�y�7�ѣG�̙3ͤI������3�O=�T���n�z�)�k�.�p�B��NJ��6m���؄a(�ٳgOt��i���|����hhh((�˦����{����3��sO�C�:�_�k�������s�)�|X�V��_C�6���2o~G���3����y۶m��o4�\s�) r�ܹse.|-[�,�`~/N� GQdU(h!Z� |E���J��h�&�u�sާ�ES�T���æ��ׯh�o�>���ˡ�ˠ�-��-m�{ �\p�@Mo؅u�֙���{�qN;,�ŋ�cߏ䦦&3u�T���)��MS�yrP���B|o���Ԏ�l1�C&L8�����j�j/_4ϯ���4��o�o��w�>|8�5���M�P�;f���͆ Ҿ&N�hJM%C�,Z�� Z=�9螞�=~��4�7H�������@���-"0�jWW�1���{[[[�:;'�Θ1�0�+�^h�h�>��#�Aw�;�� ��Ba�J%�ޓ���;�����с� ���4�2�<xͩt�8��="" p�ٴi�����="" 4g�q8��:��u�9q:v�i��x(���r�b��m��n��f�ml���="" ���)��n�="" ="" -��="" ���o����+�itj�_⧟~"Ȣ="" Ԯj��`�!�x��sn��n�g��'�j��cmv��o="" !�����?������޷r���p��(��)�,Ԭ^�z�0k֬���ŀh�`�5�'��1�����&\��+�2�o="" �v��4�="" �ac="" ��="" 4�f+�e�="" �ӭ�w����j���q�#�)��t̟??��x4�7e���oh�6o޼���r&�lh�z�n��l�8��pn���kἎbhj�e�-�|!�*���ɪ��z="">�E��Ŵ��;���Ʃ�0����j\�]o^�X�A�qο�8cVh�Q�M�x��F](L3�@'f�T�*4�IxZ,K8��͜ ! ���S��MJ�h� 5��2��p�!��wۇ�n� �M�/Z�c�=&Q��_h&�8� �X8��.��鑪&uRL���b�j�~Wg���A �d@MUG(�+�B_r�$�h�w ���i 2���ʠ>�QƸ�v�e�n�~|�fm�1� D��6K�w{����z��7T0�����}���ĩ@��Q�8K�Q�"�8�^2��d�N�+l�$j3�j����h'�x�V.��qmA�����P�?[� ^bIFE�Q����#�{i���o��� �:�<&��Y���Ѳ%�L�U��܍�����ź�ZB�\���*N��X� G!*=w�J@-���k5� m��\ 7�8O~��,��=�݄}Jp�?�P�L)�P��j4F�����"Ds:��I�o���^{M����*4H#�

And I used this code for downloading

$fn = $path.'/'.$file_name;

$mm_type="application/octet-stream";
header("Cache-Control: public, must-revalidate");
header("Pragma: hack");
header("Content-Type: " . $mm_type);
header("Content-Length: " .(string)(filesize($fn)) );
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header("Content-Transfer-Encoding: binary\n");
readfile($fn);
hakre
  • 193,403
  • 52
  • 435
  • 836
Hamender
  • 85
  • 2
  • 15
  • can I know what is the value of the `$file_name` variable when you meet the problem? I have tried with your code and found no error. You can add `var_dump($fn); die()` before `$mm_type=....` and give me the output. – Hieu Le Jul 18 '13 at 10:24
  • @Trung-HieuLe $fn=http://thetexturemill.com/wp-content/uploads/2013/07/dell.png and $file_name=dell.png – Hamender Jul 18 '13 at 10:27
  • My code work with your file. You can take a look at my answer and tell me the result. – Hieu Le Jul 18 '13 at 10:42
  • @Trung-HieuLe give me your answer which work proper – Hamender Jul 18 '13 at 10:45
  • @Hamender it is at bellow http://stackoverflow.com/a/17721224/1399505 – Hieu Le Jul 18 '13 at 10:47

3 Answers3

1

The content type is wrong

application/octet-stream

Ocet-stream is used for executable files which images are not for sure.

A proper type for a image for jpg image is for example:

image/jpeg

You can use mime_content_type() to get proper content type of file

Returns the MIME content type for a file as determined by using information from the magic.mime file.

Try this code

$fn = $path.'/'.$file_name;
$mime = mime_content_type($fn);
header('Content-Type:'.$mime);
header('Content-Length: ' . filesize($fn));
readfile($fn);
Robert
  • 19,800
  • 5
  • 55
  • 85
1

Try this,

<?php
    $fn = $path.'/'.$file_name;
    $mm_type="application/octet-stream";
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header("Content-Length: " .(string)(filesize($fn)) );
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Disposition: attachment; filename="'.$fn.'"');
    header("Content-Transfer-Encoding: binary\n");
    ob_clean();
    flush();
    readfile($fn);
    exit;
?>

Read this http://php.net/manual/en/function.readfile.php

Rohan Kumar
  • 40,431
  • 11
  • 76
  • 106
1

I have tried with your code and found no problem. After read your comment and try with your file thetexturemill.com/wp-content/uploads/2013/07/dell.png I have this code working:

# my demo value in my local machine
$path = dirname(__FILE__) . "/demo";
$file_name = "Capture.PNG";    
#$fn = realpath($path.'/'.$file_name);
$fn = "http://thetexturemill.com/wp-content/uploads/2013/07/dell.png";
//var_dump(readfile($fn));

$mm_type="application/octet-stream";
#$mm_type=mime_content_type($fn);
#echo $mm_type; die();
ob_get_flush();
header("Cache-Control: public, must-revalidate");
header("Pragma: hack");
header("Content-Type: " . $mm_type);
#header("Content-Length: " .(string)(filesize($fn)) );
header('Content-Disposition: attachment; filename="'.$file_name.'"');
header("Content-Transfer-Encoding: binary\n");
readfile($fn);
flush();
die();

What problems I found are:

  • If you use an image from remote host, make sure you can get it (the allow_url_fopen INI option is ON and the returned value from readfile is greater than zero) and do not use filesize as well as mime_content_type functions.
  • I don't know whether thetexturemill.com is your domain name or folder name. Supposed that it is a domain name, remember to add the protocal prefix (http:// as in example)
  • Do not output anything before the header function calls or your downloaded file will not be open properly.

Ah, for local file, your original code work without errors on my machine.

Hieu Le
  • 8,288
  • 1
  • 34
  • 55
  • @Trun-HieuLe when i execute on localhost this warning with same charactor Warning: Cannot modify header information - headers already sent by (output started at /opt/lampp/htdocs/download/downloading.php:12) in /opt/lampp/htdocs/download/downloading.php on line 13 Warning: Cannot modify header information - headers already sent by (output started at /opt/lampp/htdocs/download/downloading.php:12) in /opt/lampp/htdocs/download/downloading.php on line 14 – Hamender Jul 18 '13 at 11:10
  • Can you tell me exactly the location of the PHP download file and how you use it in wordpress. Do you use it in a plugin or a theme? This could help a lot. – Hieu Le Jul 18 '13 at 14:28