Download a file named with an apostrophe using PHP

They have: 1 posts

Joined: Aug 2009

I'm trying to modify a well know script for downloading files - to allow a filename with an apostrophe in it's name... such as [Fred's file.mp3] - These files must contain an apostrophe to make sense. Is there a workaround for this?

If the filename has an apostrophe, the script just does nothing.

Here's the code snippet:

if($_GET['download'] && $forcedownloads) {
$file = str_replace('/', '', $_GET['download']);
$file = str_replace('..', '', $file);

if(file_exists($leadon . $file)) {
header("Content-type: application/x-download");
header("Content-Length: ".filesize($leadon . $file));
header('Content-Disposition: attachment; filename="'.$file.'"');
readfile($leadon . $file);
die();
}
}

They have: 1 posts

Joined: Oct 2009

I've taken this same script and over a year had to change a few things to get it to work for me, the apostrophe problem being one. Early in the routine add this:

$file = stripslashes($file);

PHP will add slashes to your file name and this function is the final step so-to-speak of formatting the file name you're requesting from the server. Yes you could simply put the stripslashes() within one of your other lines of code.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.