drf65218 2012-02-06 03:59
浏览 9
已采纳

PHP标头在使用变量时返回空格?

I have asked a question earlier but I think I missed the point so I'm gonna post it again.

I'm using the header Content-Disposition.

The code I used to do so was like this:

header("Content-Disposition: attachment; filename='{$name}'");

It returns this:

Content-Disposition attachment; filename=' Bassnectar - Bass Head Official.mp3 '

Note the spaces before and after the quotes. This is a problem because it changes the extension from ".mp3" to ".mp3 " which is recognized as a different file. There has to be some way to fix this. I've changed everything, I've googled everything. I don't know what is left to do?

  • 写回答

2条回答 默认 最新

  • doushihu5475 2012-02-06 04:05
    关注

    You should url encode and trim extra whitespace before placing in the header.

    $name = rawurlencode(trim($name));
    header("Content-Disposition: attachment; filename='{$name}'");
    

    Final Solution

    $name = $extract['original_name'];
    $name2 = urlencode(trim($name));
    $name3 = str_replace("+", " ", $name2);
    $quote = '"';
    header("Content-Disposition: attachment; filename=$quote{$name3}$quote");
    

    I am pretty sure you could clean this up:

    $name = $extract['original_name'];
    $name = urlencode(trim($name));
    $name = str_replace("+", " ", $name);
    header("Content-Disposition: attachment; filename=\"{$name}\"");
    

    And, if the urlencode does not seem to make a difference, I am pretty certain you can just do this:

    $name = $extract['original_name'];
    $name = trim($name);
    
    header("Content-Disposition: attachment; filename=\"{$name}\"");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度