0

I have serialize an image path and saved to database when i am unserilazing its not working with php 7.2 before it was working with php 5.6 .

a:1:{i:0;a:3:{s:8:"fullpath";s:77:"https://www.educationfolder.com/files1/user-pics/4/142944540636159131_ml.jpg";s:5:"image";s:27:"4/142944540636159131_ml.jpg";s:10:"mention_id";s:3:"398";}}


function made for this are 
function maybe_serialize( $data ) {
if ( is_array( $data ) || is_object( $data ) )
    return serialize( $data );
// Double serialization is required for backward compatibility.
// See http://core.trac.wordpress.org/ticket/12930
if ( is_serialized( $data, false ) )
    return serialize( $data );
return $data;
}

function maybe_unserialize( $original ) {
if ( is_serialized( $original ) ) // don't attempt to unserialize data that wasn't serialized going in
    return @unserialize( $original );
return $original;
}

now i am doing it like

$imageArr = array();

$imageArr = maybe_unserialize(trim($image_url));

and its not working

3
  • 2
    What do you mean "not working?" Commented Jan 27, 2019 at 8:06
  • i have realized we have replaced "uploads" to "files1" thats why count of image characters is coming 77 in serialized string but in actual characters are 76, this need to fix to remove 1 values in minus there Commented Jan 27, 2019 at 10:40
  • Is your database column a binary type? You cannot store the output of serialize() in e.g. VARCHAR. Commented Jan 27, 2019 at 13:57

3 Answers 3

1

I have resolved this using quick fix in function , this error were coming due to changed serialized data length .

$data = preg_replace_callback('!s:(\d+):"(.*?)";!', function($m) { return 's:'.mb_strlen($m[2]).':"'.$m[2].'";'; }, $data);

function maybe_unserialize( $original ) {
if ( is_serialized($original) ) // don't attempt to unserialize data that wasn't serialized going in

$original = preg_replace_callback('!s:(\d+):"(.*?)";!', function($m) { return 's:'.mb_strlen($m[2]).':"'.$m[2].'";'; }, $original);
    return @unserialize($original);
return $original;

}

Sign up to request clarification or add additional context in comments.

Comments

0

Did you edit the serialized string manually? The image url is 76 chars but in your serialized string says 77.

$img = 'a:1:{i:0;a:3:{s:8:"fullpath";s:76:"https://www.educationfolder.com/files1/user-pics/4/142944540636159131_ml.jpg";s:5:"image";s:27:"4/142944540636159131_ml.jpg";s:10:"mention_id";s:3:"398";}}';

$img = maybe_unserialize($img);

echo $img[0]['fullpath'];

By the way, those are WordPress Core's functions. The @ before unserialize suppresses the warning. You can use unserialize() directly without the @ with your original serialized string and get:

Notice: unserialize(): Error at offset 112 of 190 bytes

Example from 3v4l:

https://3v4l.org/3NWlD

3 Comments

I got your point the thing is that we have changed serialized data "uploads" directory has been replaced to "files1" , is that any way i can correct the entire table values
But did you manually change the database? You can't change a serialized string without recalculating the length. Restore your database backup, fetch all the records that you want to edit, then for each record unserialize the string, edit it, serialize it again and finally update the record in the database.
yes i changed data with query in mysql and have fixed this recalculating string
0

If you often use regular expressions, you might use T-Regx tool:

pattern('s:(\d+):"(.*?)";')->replace($original)->callback(function(Match $m) { 
    return 's:' . $m->group(2)->length() . ':"' . $m->group(2) . '";'; 
});

It has automatic delimiters.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.