Trill Designs: Storing Images In Xml File - Part Ii - Displaying Image - Trill Designs

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Storing Images In Xml File - Part Ii - Displaying Image

#1 User is offline   Jaan Icon

  • Site Admin
  • Icon
  • Group: Administrators
  • Posts: 85
  • Joined: 23-November 09
  • LocationEstonia

Posted 25 January 2010 - 09:10 PM

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'];

if(is_numeric($id)){
	
	$id = addslashes(htmlentities(htmlspecialchars($_GET['id'])));	
	
}else{
	
	die("Wrong ID!");
	
}


$id = $_GET['id']; - Let's ger image's 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);


$xml = new SimpleXMLElement("image.xml", null, true); - It turns our XML file into an object

And now we must get our file type and image's content from XML file.

$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);
		
	}
	
}


$num = $id-1; - It decreases our id by one number because counting things in XML starts from 0

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:

Posted Image

And we are done.. :) Here's the full 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);
		
	}
	
}

?>


I hope you liked it and you enjoyed reading it..

Regards,
Jaan
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users