duanou8504 2013-07-18 09:59
浏览 124
已采纳

下载代码在wordpress中不起作用[重复]

This question already has an answer here:

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�
  • 写回答

3条回答 默认 最新

  • duanbu1998 2013-07-18 10:41
    关注

    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
    ");
    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 深度学习中梯度为None
  • ¥15 zynq烧写程序到flash后不运行
  • ¥15 在ECharts中绘制饼图及环形图(图表)
  • ¥15 (标签-51单片机|关键词-#include)
  • ¥15 裸板ESD问题的思考与实验
  • ¥15 electron+python
  • ¥50 远程调试PLC的解决方案的问题
  • ¥15 微信收付通中,自动分账的情况下,某一接收方分账失败了系统会自动在发起重新分账吗
  • ¥15 宝塔ssl证书一直待待验证
  • ¥15 相近语义 不同表达 匹配