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 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊