使用变量时 PHP 标头返回空格?
我之前问过一个问题,但我想我错过了重点,所以我会再次发布。
我正在使用标题 Content-Disposition。
我用来这样做的代码是这样的:
header("Content-Disposition: attachment; filename='{$name}'");
它返回:
内容-处置附件; filename='Bassnectar - 低音头官方.mp3'
注意引号前后的空格。这是一个问题,因为它将扩展名从“.mp3”更改为“.mp3”,后者被识别为不同的文件。必须有某种方法来解决这个问题。我改变了一切,我用谷歌搜索了一切。我不知道还剩下什么可做的?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在放入标头之前,您应该进行 url 编码并修剪多余的空格。
最终解决方案
我非常确定您可以解决这个问题:
而且,如果 urlencode 似乎没有什么区别,我非常确定您可以这样做:
You should url encode and trim extra whitespace before placing in the header.
Final Solution
I am pretty sure you could clean this up:
And, if the urlencode does not seem to make a difference, I am pretty certain you can just do this:
尝试这些:
或
或
最后一个可能最适合您。
这些将从字符串中删除空格,只需确保有相应的文件可以匹配,
Try these:
or
or
This last one works probably the best for you.
Those will remove the spaces from your string, just make sure there's a corresponding file to match,