cannot unserialize serialized getimagesize array from file
<p>I have a file, "serialized.txt", which contains a serialized array (created by doing serialize($array)). </p>

<pre><code>s:133:"a:7:{i:0;i:640;i:1;i:480;i:2;i:2;i:3;s:24:"width="640" height="480"";s:4:"bits";i:8;s:8:"channels";i:3;s:4:"mime";s:10:"image/jpeg";}";
</code></pre>

<p>To fetch the contents I do:</p>

<pre><code>$string = file_get_contents("serialized.txt");
</code></pre>

<p>Then I do:</p>

<pre><code>print_r(unserialize($string));
</code></pre>

<p>The output that I get:</p>

<pre><code>a:7:{i:0;i:640;i:1;i:480;i:2;i:2;i:3;s:24:"width="640" height="480"";s:4:"bits";i:8;s:8:"channels";i:3;s:4:"mime";s:10:"image/jpeg";}
</code></pre>

<p>This is the unserialized version of the string (contents of the file) when it should be printing the unserialized array. If I copy the string and do the following:</p>

<pre><code>print_r(unserialize('a:7:{i:0;i:640;i:1;i:480;i:2;i:2;i:3;s:24:"width="640" height="480"";s:4:"bits";i:8;s:8:"channels";i:3;s:4:"mime";s:10:"image/jpeg";}'));
</code></pre>

<p>I get the correct output:</p>

<pre><code>Array
(
    [0] =&gt; 640
    [1] =&gt; 480
    [2] =&gt; 2
    [3] =&gt; width="640" height="480"
    [bits] =&gt; 8
    [channels] =&gt; 3
    [mime] =&gt; image/jpeg
)
</code></pre>

<p>So the problem seems to be isolated to the serialized array when pulling from the file.</p>

<p>According to the <a href="http://php.net/manual/en/function.unserialize.php" rel="nofollow">unserialize</a> docs the function should be returning <strong>false</strong> if there is a problem; not the contents of the string.</p>

<p>The serialized data is taken from <a href="http://php.net/manual/en/function.getimagesize.php" rel="nofollow">getimagesize</a> and I have verified that if I serialize another array and place it into the file:</p>

<pre><code>serialize(array("hi"));
</code></pre>

<p>I can successfully generate the output:</p>

<pre><code>Array
(
    [0] =&gt; hi
)
</code></pre>

<p>Are there any ideas why this may be happening? A bug with the serialization process relating to a getimagesize array, or potentially a "hidden" character in the file that my copy and paste removes? I have millions of these files already generated so it's not possible for me to change the storage method. I guess the solution may just be to write my own parser to serialize the array? The input is always the same format so that's plausible, but I would like to know of this a bug or my error with something somewhere.</p>
