Closed Thread
Results 1 to 7 of 7

Thread: Storing images in XML file - Part II - Displaying image

  1. #1
    Jaan Guest

    Storing images in XML file - Part II - Displaying image

    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.

    Code:
    $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.

    Code:
    $xml = new SimpleXMLElement("image.xml"nulltrue); 
    $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.

    Code:
    $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:



    And we are done.. Here's the full code:

    Code:
    <?php

    $id 
    $_GET['id'];

    if(
    is_numeric($id)){

    $id addslashes(htmlentities(htmlspecialchars($_GET['id'])));    

    }else{

    die(
    "Wrong ID!");

    }

    $xml = new SimpleXMLElement("image.xml"nulltrue);

    $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..
    Last edited by Roger; 01-04-2011 at 08:35 PM.

  2. CODECALL Circuit advertisement

     
  3. #2
    Jordan Guest

    Re: Storing images in XML file - Part II - Displaying image

    Retrieving it is fairly simple! Excellent Tutorial. +rep.

  4. #3
    Jaan Guest

    Re: Storing images in XML file - Part II - Displaying image

    Eheh.. thank you

  5. #4
    Join Date
    Mar 2008
    Posts
    7,140
    Rep Power
    86

    Re: Storing images in XML file - Part II - Displaying image

    Very nice! Why does the below code work to display the image?

    Code:
    echo base64_decode($xml->image[$num]->content);

  6. #5
    Jaan Guest

    Re: Storing images in XML file - Part II - Displaying image

    it's like..

    Code:
    <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>
    this is my XML and then I use PHP to get url to my bike's picture

    Code:
    echo $xml->image[2]->url
    and it prints http://myimage.com/my_bike.jpg

    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 me because I'm really tired and my english sucks a koala

  7. #6
    Faizan is offline Newbie
    Join Date
    Jun 2010
    Posts
    1
    Rep Power
    0

    Post Re: Storing images in XML file - Part II - Displaying image

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

  8. #7
    Join Date
    Jun 2010
    Location
    USA
    Posts
    574
    Blog Entries
    5
    Rep Power
    20

    Re: Storing images in XML file - Part II - Displaying image

    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.
    Wrap [Code] tags when posting code by click on
    Before posting your question, did you look here?

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. storing images in database
    By ravi951 in forum Database & Database Programming
    Replies: 6
    Last Post: 08-21-2011, 03:11 PM
  2. Replies: 213
    Last Post: 04-14-2011, 07:57 PM
  3. Tutorial: Storing Images in MySQL with PHP
    By Jordan in forum PHP Tutorials
    Replies: 47
    Last Post: 01-04-2011, 08:37 PM
  4. storing and load images using linq to sql
    By aglayo2010 in forum C# Programming
    Replies: 0
    Last Post: 09-25-2010, 08:40 PM
  5. Replies: 2
    Last Post: 07-16-2009, 12:48 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts