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
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
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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.