Page 1 of 1

Easy question regarding quotation marks in php

Posted: Tue Apr 13, 2004 12:37 pm
by Chris Corbyn
Hi,

I've got a problem redirecting to a file because my filename has a " ' " (apostrophe) in it.

If I need to redirect to "It's_Some_File.txt" the php tries to read "It.txt" and save "It.txt" using for example this code below.

Code: Select all

$file = "It's_Some_File";

header ('Content-type: Text/Plain');
header ('Content-Disposition: attachment; filename="'.$file.'.txt"');
readfile (''.$file.'.txt');
I know that it's just because as soon as it hits the ' it just stops reading the filename but with echo etc you can play around with where you put quotation marks and which kind you use to make sure it still outputs the entire string.

Is there a way to get my reidrect to work?

Posted: Tue Apr 13, 2004 12:54 pm
by RadixDev
Use URL encoding...
so your file name would be "It%27s%5FSome%5FFile.txt".
Check this out - http://www.w3schools.com/html/html_ref_urlencode.asp

Posted: Tue Apr 13, 2004 1:01 pm
by Chris Corbyn
Oh yeah, thanks, I never even thought of that. That'll definitely work :-)

Posted: Tue Apr 13, 2004 1:44 pm
by Chris Corbyn
I got it to work but urlencode(); didn't work since it tried to open a file with the encoded filename (doesn't exist and I'm not gonna change my filenames).

I used the same principle though, with htmlentities($somestring, ENT_QUOTES);

This worked a treat. The other advantage with htmlentities is that it doesn't try to save the file with a name full of encoded values, it just saves what it is actually called.

Thanks for getting me thinking in the right direction.

Posted: Tue Apr 13, 2004 4:12 pm
by RadixDev
Forgot about URL encoding functions in PHP LOL :lol:
But you're welcome, guess you learn new things everyday! :wink: