Page 1 of 1

Weird issue with fread or file_get_contents

Posted: Wed Dec 09, 2009 7:52 pm
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

Re: Weird issue with fread or file_get_contents

Posted: Wed Dec 09, 2009 9:03 pm
by requinix
Do you have magic_quotes_runtime (an INI setting) turned on? Are you able to modify the php.ini?

Re: Weird issue with fread or file_get_contents

Posted: Thu Dec 10, 2009 6:29 am
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.

Re: Weird issue with fread or file_get_contents

Posted: Thu Dec 10, 2009 8:17 am
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.