Heey ^^
This is last part of our storing images in XML file tutorial and it's how to display our image from XML file.
Don't worry, this isn't that long
But let's start.
First let's check do our file's id is a number or not.
$id = $_GET['id']; - Let's ger image's idCode:$id = $_GET['id']; if(is_numeric($id)){ $id = addslashes(htmlentities(htmlspecialchars($_GET['id']))); }else{ die("Wrong ID!"); }
if(is_numeric($id)){ - If our id is a number ..
$id = addslashes(htmlentities(htmlspecialchars($_GET['id']))); - .. let's make it more secure
}else{ - If it's not ..
die("Wrong ID!"); - .. let's display an error
} - End else
Now it's time to get our XML file.
$xml = new SimpleXMLElement("image.xml", null, true); - It turns our XML file into an objectCode:$xml = new SimpleXMLElement("image.xml", null, true);
And now we must get our file type and image's content from XML file.
$num = $id-1; - It decreases our id by one number because counting things in XML starts from 0Code:$num = $id-1; foreach($xml->children() as $i){ if($i['id'] == $id){ $type = $xml->type; header('Content-type: $type'); echo base64_decode($xml->image[$num]->content); } }
foreach($xml->children() as $i){ - Let's get all childs from our XML file
if($i['id'] == $id){ - If id="" is this number that we want - that what's our id..
$type = $xml->type;
header('Content-type: $type');
echo base64_decode($xml->image[$num]->content); - Let's get everything we need. Type and content. Did you noticed I decoded our image's content so it looks like we took it just from it's original file
} - End if
} - End foreach
If everything is correct you should see something like this:
And we are done..Here's the full code:
I hope you liked it and you enjoyed reading it..Code:<?php $id = $_GET['id']; if(is_numeric($id)){ $id = addslashes(htmlentities(htmlspecialchars($_GET['id']))); }else{ die("Wrong ID!"); } $xml = new SimpleXMLElement("image.xml", null, true); $num = $id-1; foreach($xml->children() as $i){ if($i['id'] == $id){ $type = $xml->type; header('Content-type: $type'); echo base64_decode($xml->image[$num]->content); } } ?>
Regards,
Jaan
Bookmarks