Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

php xml frustration - whats wrong here?

  • 15-10-2009 10: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,317 ✭✭✭✭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