php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #25185 Unserialize can't decode string from serialize
Submitted: 2003-08-21 05:14 UTC Modified: 2003-08-21 17:31 UTC
From: cpuidle at gmx dot de Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 4.3.3RC4 OS: Windows
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: cpuidle at gmx dot de
New email:
PHP Version: OS:

 

 [2003-08-21 05:14 UTC] cpuidle at gmx dot de
Description:
------------
I'm using serialize to write array structures to the filesystem. After reading the structure again, unserialize fails to decode and returns 0 instead. Serialized sample data is included. The problem is fixed when the two larger data blocks are removed and replaced with samething without linebreaks- not sure if that's part of the problem.

Reproduce code:
---------------
http://www.cpuidle.de/serialized.txt



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-08-21 10:06 UTC] helly@php.net
The problem doesn't have to do anything with serialize/unserialize but with the way you store/load the serialized data. Can you tell us how? And do you do any manual changes to the serialized data or even view it with an external editor?
 [2003-08-21 14:46 UTC] cpuidle at gmx dot de
This is the code to save and restore the data:

function download($url, $local){
  $resp = fetchHTTP($url);
	$writefile = @fopen($local, "wb");
	if(!$writefile) return false;
	if(!fwrite($writefile, $resp[data], strlen($resp[data]))) return false;
	fclose($writefile);
	return true;
}

function getHTTPcache($url){
  $cfile='../videodb/cache/imdb/'.md5($url);
    $resp = unserialize(join('',file($cfile)));
    return $resp;
}
 [2003-08-21 14:47 UTC] cpuidle at gmx dot de
Have not tried the snapshot yet- is it supposed to fix (un)serialize or load/store?

Andi
 [2003-08-21 14:59 UTC] cpuidle at gmx dot de
I'm sorry. The function where the file is written is obviously not download, but this one:

function saveHTTPcache($url,$resp){
  $cfile='cache/imdb/'.md5($url);
  $string=serialize($resp);
  @fwrite(@fopen("$cfile", "w"),$string,strlen($string));
}
 [2003-08-21 15:06 UTC] cpuidle at gmx dot de
The filereading hint was a good suggestion- it does actually seem to work if I'm using file_get_contents instead of join('',file($file))-
could smdb. explain why?

Kindly,
Andi
 [2003-08-21 17:31 UTC] helly@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

The way you save and load the data must match. Windows does line termination translation (e.g. \n -> \r\n). To avoid this you must write and read using open mode 'wb'.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Sep 19 15:01:27 2024 UTC