Jump to content

unserialize - simplexml


werty37

Recommended Posts

HI

Can anyone help me out with this please.

 

I am trying to print out serialized data stored in a xml file using simplexml.

 

$xml = simplexml_load_file($conf);
echo (string) $xml->file->allowedfiles; //prints out the serialized data
unserialize((string) $xml->file->allowedfiles); //doesnt print any output

 

Thanks

Sujith

Link to comment
https://forums.phpfreaks.com/topic/99936-unserialize-simplexml/
Share on other sites

Hi Barand,

 

$xml = simplexml_load_file('conf.xml');
echo (string) $xml->file->allowedfiles; //prints out the serialized data
$data = unserialize((string) $xml->file->allowedfiles); 
echo '<pre>', print_r($data, 1), '</pre>'; // dosent print anything

 

It outputs nothing.

a:8:{s:4:".jpg";s:11:"image/pjpeg";s:4:".gif";s:9:"image/gif";s:4:".bmp";s:9:"image/bmp";s:4:".png";s:11:"image/x-png";s:4:".swf";s:29:"application/x-shockwave-flash";s:4:".mp3";s:11:"audio/mpeg3";s:4:".avi";s:9:"video/avi";s:4:".zip";s:15:"application/zip";}

< pre ></ pre >

 

The < pre > tag is printed but not the unserialized content..

 

Sujith

Link to comment
https://forums.phpfreaks.com/topic/99936-unserialize-simplexml/#findComment-511080
Share on other sites

Hmm, strange.

 

I tried

<?php
$ser = 'a:8:{s:4:".jpg";s:11:"image/pjpeg";s:4:".gif";s:9:"image/gif";s:4:".bmp";s:9:"image/bmp";s:4:".png";s:11:"image/x-png";s:4:".swf";s:29:"application/x-shockwave-flash";s:4:".mp3";s:11:"audio/mpeg3";s:4:".avi";s:9:"video/avi";s:4:".zip";s:15:"application/zip";}
';
$unser = unserialize($ser);

echo '<pre>', print_r($unser, true), '</pre>';
?>

gives -->
Array
(
    [.jpg] => image/pjpeg
    [.gif] => image/gif
    [.bmp] => image/bmp
    [.png] => image/x-png
    [.swf] => application/x-shockwave-flash
    [.mp3] => audio/mpeg3
    [.avi] => video/avi
    [.zip] => application/zip
)

Link to comment
https://forums.phpfreaks.com/topic/99936-unserialize-simplexml/#findComment-511123
Share on other sites

Hi

 

Just a reminder. Simplexml returns objects instead of strings/integers or mixed values.

 

echo is_object($xml->file->allowedfiles); // will print 1

 

That is why i do type cast before i print

 

echo unserialize((string) $xml->file->allowedfiles); //doesnt print any output

 

I think the problem has something to do with this behaviour. or maybe i have found a bug in php :D

Link to comment
https://forums.phpfreaks.com/topic/99936-unserialize-simplexml/#findComment-511320
Share on other sites

The following worked fine for me:

 

<config>
<file>
	<allowedfiles>a:8:{s:4:".jpg";s:11:"image/pjpeg";s:4:".gif";s:9:"image/gif";s:4:".bmp";s:9:"image/bmp";s:4:".png";s:11:"image/x-png";s:4:".swf";s:29:"application/x-shockwave-flash";s:4:".mp3";s:11:"audio/mpeg3";s:4:".avi";s:9:"video/avi";s:4:".zip";s:15:"application/zip";}</allowedfiles>
</file>
</config>

 

<?php
  $xml = simplexml_load_file('conf.xml');
  echo (string) $xml->file->allowedfiles; //prints out the serialized data
  $data = unserialize((string) $xml->file->allowedfiles); 
  echo '<pre>', print_r($data, 1), '</pre>'; // dosent print anything
?>

 

a:8:{s:4:".jpg";s:11:"image/pjpeg";s:4:".gif";s:9:"image/gif";s:4:".bmp";s:9:"image/bmp";s:4:".png";s:11:"image/x-png";s:4:".swf";s:29:"application/x-shockwave-flash";s:4:".mp3";s:11:"audio/mpeg3";s:4:".avi";s:9:"video/avi";s:4:".zip";s:15:"application/zip";}

Array
(
    [.jpg] => image/pjpeg
    [.gif] => image/gif
    [.bmp] => image/bmp
    [.png] => image/x-png
    [.swf] => application/x-shockwave-flash
    [.mp3] => audio/mpeg3
    [.avi] => video/avi
    [.zip] => application/zip
)

 

Update: Using PHP Version 5.2.5

Link to comment
https://forums.phpfreaks.com/topic/99936-unserialize-simplexml/#findComment-511327
Share on other sites

Since you are already using XML, you could always store it as XML too...

 

 

<config>
<file>
	<allowedfiles>
		<filetype ext="jpg">image/pjpeg</filetype>
		<filetype ext="gif">image/gif</filetype>
		<filetype ext="bmp">image/bmp</filetype>
		<filetype ext="png">image/x-png</filetype>
		.....
	</allowedfiles>
</file>
</config>

Link to comment
https://forums.phpfreaks.com/topic/99936-unserialize-simplexml/#findComment-511347
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.