Easy question regarding quotation marks in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Easy question regarding quotation marks in php

Post 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?
RadixDev
Forum Commoner
Posts: 66
Joined: Sun Mar 14, 2004 11:27 am
Location: U.K.

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Oh yeah, thanks, I never even thought of that. That'll definitely work :-)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
RadixDev
Forum Commoner
Posts: 66
Joined: Sun Mar 14, 2004 11:27 am
Location: U.K.

Post by RadixDev »

Forgot about URL encoding functions in PHP LOL :lol:
But you're welcome, guess you learn new things everyday! :wink:
Post Reply