Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

php xml frustration - whats wrong here?

  • 15-10-2009 11:16PM
    #1
    Registered Users, Registered Users 2 Posts: 21


    hi,
    im trying to get a html/php form to either generate or add to an xml file depending on whether or not that file is already created.

    For the file not existing, i have this to create the xml file:

    [php]$xml = simplexml_load_file("./xml_files/".$user_id.".xml");

    $item = new SimpleXMLElement("<items></items>");
    $hi = $item->addChild("item");
    $hi->addChild("itemName",$item_n);
    $hi->addChild("itemSource",$source);
    $item->asXML("./xml_files/".$user_id.".xml");
    [/php]this generates the xml when it's executed.


    for the case where the file already exists, assuming the first tag is <items> just like the created one in the last example, i have this:
    [php]
    $xml = simplexml_load_file("./xml_files/".$user_id.".xml");
    $item = new SimpleXMLElement($xml->asXML());
    $by = $item->addChild("item");
    $by->addChild("itemName",$item_n);
    $by->addChild("itemSource",$source);
    $item->asXML("./xml_files/".$user_id.".xml");
    [/php]Both of these work on their own(ie. I remove the xml and the second method for the 1st example, or leave an xml file in the directory and delete the first method).

    The problem is when i try to combine them... as i said, the plan was to use either 1 depending on if the xml file already existed or not, so i thought i'd use the 'if (file_exists(file))' method. Here's what i have :
    [php]
    $filename = "./xml_files/".$user_id.".xml";
    if (!file_exists($filename)) {
    $xml = simplexml_load_file("./xml_files/".$user_id.".xml");

    $item = new SimpleXMLElement("<items></items>");
    $hi = $item->addChild("item");
    $hi->addChild("itemName",$item_n);
    $hi->addChild("itemSource",$source);
    $item->asXML("./xml_files/".$user_id.".xml");

    }else{

    $xml = simplexml_load_file("./xml_files/".$user_id.".xml");
    $item = new SimpleXMLElement($xml->asXML());
    $by = $item->addChild("item");
    $by->addChild("itemName",$item_n);
    $by->addChild("itemSource",$source);
    $item->asXML("./xml_files/".$user_id.".xml");
    }
    [/php]Can anyone see why this won't work or offer a suggestion please? ive been at it for hours :(
    cheers :)


Comments

  • Registered Users, Registered Users 2 Posts: 8,488 ✭✭✭Goodshape


    Have you tried using an absolute path for file_exists() ?

    if (!file_exists($_SERVER . $filename)) { stuff }


  • Registered Users, Registered Users 2 Posts: 21 prometheos


    hi,
    yeah i tried that and it works if theres no xml file already there, but if i go back and submit the form again it overwrites the xml file instead of adding to it?
    i dont think it likes my file check :(
    Its like it works with the first block nomatter which it is but never gets to the second block :(
    thanks for your suggestion by the way :)


  • Registered Users, Registered Users 2 Posts: 21 prometheos


    ok, this call seems to work ...
    [php]if (!file_exists($_SERVER.$filename)) {}[/php] call
    but this one doesnt? [php]if (file_exists($_SERVER.$filename)){}[/php]
    even if i use the not one first and put else or else if, it doesnt work :(
    any ideas?


  • Registered Users, Registered Users 2 Posts: 68,173 ✭✭✭✭seamus


    Check out the notes in the manual on this function:
    http://ie2.php.net/manual/en/function.file-exists.php

    Particularly the notes about safe mode and caching.

    Might help.


  • Registered Users, Registered Users 2 Posts: 21 prometheos


    hey thanks for the suggestion,
    safe mode is off on the server im using... :S
    Can anyone think of another way of going about this maybe?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 21 prometheos


    finally, headache gone!
    i decided to not use the file_exists method.
    my solution was this...
    [php]
    $filename = "./xml_files/".$user_id.".xml";
    $file = fopen ($filename, "r");
    if (!$file){
    $xml = simplexml_load_file(filename);

    $item = new SimpleXMLElement("<items></items>");
    $hi = $item->addChild("item");
    $hi->addChild("itemName",$item_n);
    $hi->addChild("itemSource",$source);
    $item->asXML($filename);

    }else{

    $xml = simplexml_load_file($filename);
    $item = new SimpleXMLElement($xml->asXML());
    $by = $item->addChild("item");
    $by->addChild("itemName",$item_n);
    $by->addChild("itemSource",$source);
    $item->asXML($filename);
    }
    fclose($file);
    [/php]

    Thanks for the help guyz


Advertisement