Weird issue with fread or file_get_contents

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
tormit
Forum Newbie
Posts: 3
Joined: Wed Dec 09, 2009 7:27 pm
Location: Estonia

Weird issue with fread or file_get_contents

Post by tormit »

I got some really weird issue with fread or file_get_contents. For example I'm reading javascript file and I'm getting string literals backslashed or escaped.
For example:

Code: Select all

typeof text == "string"
gets read as:

Code: Select all

typeof text == \"string\"
What makes things complicated is that it happens in Symfony framework project. It does not happen when I create one simple PHP file to do this operation.
But Symfony's any automatic escaping mechanism cannot or should not cause this behavior, because

Code: Select all

// when I read file
$code = file_get_contents($file);
 
// and for testing, directly after reading, I write out the read content, IT IS ESCAPED
file_put_contents('read_asset_code.txt',  date('Y-m-d H:i:s').': '.$file.': '.$code, FILE_APPEND);
How can this issue happen? No escaping mechanism cannot apply on code above according to my knowledge.
And no matter whether I use file_get_contents or fread.

Can anyone help? :D
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Weird issue with fread or file_get_contents

Post by requinix »

Do you have magic_quotes_runtime (an INI setting) turned on? Are you able to modify the php.ini?
tormit
Forum Newbie
Posts: 3
Joined: Wed Dec 09, 2009 7:27 pm
Location: Estonia

Re: Weird issue with fread or file_get_contents

Post by tormit »

I have all magic_quotes_* turned off.

I forgot to mention that I'm using PHP 5.2.11, latest 5.2 stable.
tormit
Forum Newbie
Posts: 3
Joined: Wed Dec 09, 2009 7:27 pm
Location: Estonia

Re: Weird issue with fread or file_get_contents

Post by tormit »

SOLVED

Thanks for directing my attention to magic_quotes_runtime. It was not set On in php.ini, but it was set On and left On on runtime by another script - specifically in PEAR -> Request2.php

So, if anyone else has similar issue, make sure that magic_quotes_runtime is turned Off before using fread or file_get_contents.
Post Reply