Jump to content

PHP, save an edited XML file


Miko

Recommended Posts

Hello,

 

I'm editing an XML file using PHP, the XML file contains a playlist of songs for a flash mp3 player.

Now I'm able to edit the XML file, but I'm not able to save it  ::).

 

Here's my code:

 

    $random_speed  = mt_rand(1,15); // Discspeed
    $song_title    = /*mysql_real_escape_string($_GET['song_title'])*/ "My First XML Edit";
    $song_artist   = /*mysql_real_escape_string($_GET['song_artits'])*/ "A great author";
    $song_filename = /*basename(mysql_real_escape_string($_GET['song_filename']))*/ "myfirstxmledit.mp3";
    
    /* Open image directory and get a random image name */
    $dir          = "./img";
    /* Scan the directory and put everything in an array */
    $files        = scandir($dir);
    /* Count the elements in the array */
    $count_array  = count($files);
    /* Pick a random index from the array */
    $rand_array   = array_rand( $files, 1 );
    /* Pick the value of the random index */
    $random_image = $files[$rand_array];
    /* Loop through array and if element is not equal to "." or ".." then set the random_image variable */
    if( ($random_image == "." ) || ($random_image == ".." ) ){
        while( ($random_image == "." ) || ($random_image == ".." ) ){
            $random_image = $files[$rand_array];
        }
    }elseif( ($random_image != "." ) || ($random_image != ".." ) ){
        echo $random_image;
    }
    /* Open the music.xml file */
    $xml = new DOMDocument('1.0', 'utf-8');
    $xml->formatOutput = true;
    $xml->preserveWhiteSpace = false;
    $xml->load('music.xml');
    /* Begin settin new element to music.xml file */
    $newItem = $xml->createElement('song');
    $newItem->appendChild($xml->createElement('title', "'$song_title'"));
    $newItem->appendChild($xml->createElement('artist', "'$song_artist'"));
    $newItem->appendChild($xml->createElement('url', "songs/'$song_filename'"));
    $newItem->appendChild($xml->createElement('image', "img/'$random_image'"));
    $newItem->appendChild($xml->createElement('discspeed', $random_speed));
    /* Add the new elements to the xml tag 'song' */
    $xml->getElementsByTagName('song')->item(0)->appendChild($newItem);
    /* Save the xml file */
    $xml->save();

 

I've tried to enter the filename in the last $xml->save() function, didn't work, added the location + filename, didn't work ...

Anyone knows what or how do I save the file?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/216703-php-save-an-edited-xml-file/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.