1

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?

1
  • 2
    Have you tried using trim() on $name? Commented Feb 6, 2012 at 4:02

2 Answers 2

1

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}\"");
Sign up to request clarification or add additional context in comments.

14 Comments

@RosengustaGarrett Sorry about that, wasn't thinking! I fixed the answer. Please try it and let me know if it solved the problem.
@RosengustaGarrett Hold up! The browser should automatically decode that correctly therefore accessing the correct file. If it does not work, then remove the urlencode() call and leave just trim()
@RosengustaGarrett Good song, btw!
@RosengustaGarrett You should not have to do the str_replace.
@RosengustaGarrett Updated the answer, please try rawurlencode instead of urlencode
|
0

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,

7 Comments

$name=preg_replace('/\s+/','',$name); Returned: Bassnectar-BassHeadOfficial.mp3 (I used a song as a test) That works good except it removed every space in the title. I could do the name thing str_replace(" ", "", $name) $string = trim(ereg_replace(' +', ' ', $string)); Returned: Bassnectar $str = preg_replace('/\s\s+/', ' ', $str); Returned: Bassnectar
Why remove all whitespaces? The problem was only the space at the beginning, I think.
@RosengustaGarrett - You only want the space removed from the extension... Can you make two variables called $name and $ext then use the space-remover for the $ext and merge it with $name like '$name' . '$ext'
@Tadeck I'm not trying to remove all white spaces just the space from the very front and very back.
@RosengustaGarrett - Very glad to hear.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.