Writing into an xml file using php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
anuantony02
Forum Newbie
Posts: 4
Joined: Mon Nov 09, 2009 5:43 am

Writing into an xml file using php

Post by anuantony02 »

I am using the flash player to display images in my page. The flash player reads the image location on the server from an xml file. The structure of the xml file is as below..

<?xml version="1.0" ?>
<album
showbuttons="yes"
showtitles="no"
showrandom="yes"
randomstart="yes"
timer="5"
aligntitles="bottom"
alignbuttons="top"
transition="blur"
speed="20"
>
<imgsrc="Images/Campus/Camp1.jpg"title="IIMBCampus" />
<img src="Images/Campus/IIMB_Logo.JPG"title="IIMB
Campus" />
<img src="Images/Campus/Camp2.jpg" title="IIMB Campus" />
<img src="Images/Campus/Camp3.jpg" title="IIMB Campus" />
<img src="Images/Campus/Camp4.jpg" title="IIMB Campus" />
<img src="Images/Campus/Camp5.jpg" title="IIMB Campus" />
<img src="Images/Campus/Camp6.jpg" title="IIMB Campus" />
<img src="Images/Campus/Camp7.jpg" title="IIMB Campus" />
<img src="Images/Campus/Camp8.jpg" title="IIMB Campus" />
<img src="Images/Campus/Camp9.jpg" title="IIMB Campus" />
<img src="Images/Campus/Camp10.jpg" title="IIMB Campus" />

</album>

So each time I upload an image its location in the server needs to be entered automatically into the xml file using php.


Please help me with this...
Thanks in advance..
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

Re: Writing into an xml file using php

Post by Grizzzzzzzzzz »

anuantony02 wrote:
<?xml version="1.0" ?>
<album
showbuttons="yes"
showtitles="no"
showrandom="yes"
randomstart="yes"
timer="5"
aligntitles="bottom"
alignbuttons="top"
transition="blur"
speed="20"
>
<imgsrc="Images/Campus/Camp1.jpg"title="IIMBCampus" />
<img src="Images/Campus/IIMB_Logo.JPG"title="IIMB
Campus" />
<img src="Images/Campus/Camp2.jpg" title="IIMB Campus" />
<img src="Images/Campus/Camp3.jpg" title="IIMB Campus" />
<img src="Images/Campus/Camp4.jpg" title="IIMB Campus" />
<img src="Images/Campus/Camp5.jpg" title="IIMB Campus" />
<img src="Images/Campus/Camp6.jpg" title="IIMB Campus" />
<img src="Images/Campus/Camp7.jpg" title="IIMB Campus" />
<img src="Images/Campus/Camp8.jpg" title="IIMB Campus" />
<img src="Images/Campus/Camp9.jpg" title="IIMB Campus" />
<img src="Images/Campus/Camp10.jpg" title="IIMB Campus" />

</album>

So each time I upload an image its location in the server needs to be entered automatically into the xml file using php.


Please help me with this...
Thanks in advance..

not the most elegant of methods...

Code: Select all

 
 
<?php 
 
$myFile = "xmlfile.xml";
 
$fh = fopen($myFile, 'r'); //open file in read mode
$theData = fread($fh, 9999999); //put all file data into a string
    
list($stuff1, $stuff2) = split("</album>", $theData); //split the '</album>' bit off the file string
 
fclose($fh); //close the file
    
$fh2 = fopen($myFile, 'w') or die("can't open file"); //open the file again, this time in write mode
    
$stuff3 = "<img src=\"Images/Campus/Camp11.jpg\" title=\"IIMB Campus\"/>"; //have the new entry you want to put into the file ready
 
$stringData = $stuff1.$stuff3.$stuff2; //slap all the info together in the correct order
 
fwrite($fh2, $stringData); //write the data into the file
fclose($fh2); //close the file
 
?>
 
anuantony02
Forum Newbie
Posts: 4
Joined: Mon Nov 09, 2009 5:43 am

Re: Writing into an xml file using php

Post by anuantony02 »

hey..thank u so much...it worked...
but i need to fetch the title of the images from the xml file and display it in front end...ie i need to match the image src and fetch the title corresponding to it. This cannot be achieved using normal file reading wrt?...i guess we need to use xml parsing for that..can anybody help me out in this. :(
Post Reply