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);
}
}
?>
Last edited by Roger; 01-04-2011 at 08:35 PM.
Retrieving it is fairly simple! Excellent Tutorial. +rep.
Eheh.. thank you![]()
Very nice! Why does the below code work to display the image?
Code:echo base64_decode($xml->image[$num]->content);
it's like..
this is my XML and then I use PHP to get url to my bike's pictureCode:<galley> <image> <title>My picture.jpg</title> <type>image/jpeg</type> <url>http://myimage.com/my_picture.jpg</url> </image> <image> <title>My dog.jpg</title> <type>image/jpeg</type> <url>http://myimage.com/my_dog.jpg</url> </image> <image> <title>My bike.jpg</title> <type>image/jpeg</type> <url>http://myimage.com/my_bike.jpg</url> </image> </gallery>
and it prints http://myimage.com/my_bike.jpgCode:echo $xml->image[2]->url;
this image[2] is like an array.. first element in array is 0 then it's 1 and 2 and so on..
and in XML first <image></image> is 0.. and when i have third <image></image> then it's 2 in numbers![]()
I hope you understanded mebecause I'm really tired and my english sucks a koala
how can we make a gallery ?. . with next and previous button n geting pictures fom xml. .?.. how can we do that ? plez tell me :S. .
The original poster is no longer with CodeCall. If you have any question regarding this posting, please start a new thread in the appropriate section of the forum (and reference this thread).
Thank you.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks