werty37 Posted April 7, 2008 Share Posted April 7, 2008 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted April 7, 2008 Share Posted April 7, 2008 $xml = simplexml_load_file($conf); echo (string) $xml->file->allowedfiles; //prints out the serialized data $data = unserialize((string) $xml->file->allowedfiles); echo '<pre>', print_r($data, 1), '</pre>'; Quote Link to comment Share on other sites More sharing options...
werty37 Posted April 7, 2008 Author Share Posted April 7, 2008 Hi Barand, Sorry the code i posted had errors. I did try echo unserialize((string) $xml->file->allowedfiles); //doesnt print any output which ofcourse does not work. Thanks Sujith Quote Link to comment Share on other sites More sharing options...
werty37 Posted April 7, 2008 Author Share Posted April 7, 2008 Bump... Quote Link to comment Share on other sites More sharing options...
Barand Posted April 7, 2008 Share Posted April 7, 2008 What did the code I posted output? Quote Link to comment Share on other sites More sharing options...
werty37 Posted April 7, 2008 Author Share Posted April 7, 2008 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 Quote Link to comment Share on other sites More sharing options...
Barand Posted April 7, 2008 Share Posted April 7, 2008 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 ) Quote Link to comment Share on other sites More sharing options...
werty37 Posted April 7, 2008 Author Share Posted April 7, 2008 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 Quote Link to comment Share on other sites More sharing options...
rhodesa Posted April 7, 2008 Share Posted April 7, 2008 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 Quote Link to comment Share on other sites More sharing options...
werty37 Posted April 7, 2008 Author Share Posted April 7, 2008 Hi Rhodesa, I am using json_d/encode() functions instead of un/serialize() and it does the job quiet well. I dont know why it wouldnt work for me. I am using PHP 5.2.3 on ubuntu (Apache/2.2.4). Thanks Sujith Quote Link to comment Share on other sites More sharing options...
rhodesa Posted April 7, 2008 Share Posted April 7, 2008 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> Quote Link to comment Share on other sites More sharing options...
werty37 Posted April 7, 2008 Author Share Posted April 7, 2008 Ah yes! That is a good idea. I ll try to implement it. Thanks a lot for the suggestion... Cheers Sujith Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.