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

发布于 2025-01-03 03:23:24 字数 385 浏览 0 评论 0原文

我之前问过一个问题,但我想我错过了重点,所以我会再次发布。

我正在使用标题 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

音盲 2025-01-10 03:23:24

在放入标头之前,您应该进行 url 编码并修剪多余的空格。

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

最终解决方案

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

我非常确定您可以解决这个问题:

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

而且,如果 urlencode 似乎没有什么区别,我非常确定您可以这样做:

$name = $extract['original_name'];
$name = trim($name);

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

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}\"");
万人眼中万个我 2025-01-10 03:23:24

尝试这些:

<?php
$str = preg_replace('/\s\s+/', ' ', $str);
?>

<?php
$string = trim(ereg_replace(' +', ' ', $string));
?>

<?php
$name=preg_replace('/\s+/','',$name);
?>

最后一个可能最适合您。

这些将从字符串中删除空格,只需确保有相应的文件可以匹配,

Try these:

<?php
$str = preg_replace('/\s\s+/', ' ', $str);
?>

or

<?php
$string = trim(ereg_replace(' +', ' ', $string));
?>

or

<?php
$name=preg_replace('/\s+/','',$name);
?>

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,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文