Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts

    Question Editing a text or xml file

    I am a complete php newbe. I have seen in several places where a php thing stores data in a text file or an xml file. Also , it uses ajax. I am pretty sure that the storing data part is using php, though. Anyone know how this is done?

    http://www.hnscripts.com/
    http://www.chromasynthetic.com/blog/archive/48

  • #2
    New Coder
    Join Date
    Nov 2007
    Posts
    41
    Thanks
    5
    Thanked 1 Time in 1 Post
    For writing files with php you should use the fopen statement. If you write a file don't forget that you need the proper permission on the file by CHMODing it to a writable level. I happened to have written a script to write a config file with gathered data from a form. Here it is:

    PHP Code:
    <?php
    $File 
    'includes/config.php';
    $Handle fopen($File'w') or die('File error');
    $Data = <<<DATA
    <?php
    \$dbhost = '
    {$_GET['dbhost']}';
    \$dbuser = '
    {$_GET['dbuser']}';
    \$dbpass = '
    {$_GET['dbpass']}';
    \$dbname = '
    {$_GET['dbname']}';
    ?>
    DATA;
    fwrite($Handle$Data);
    echo 
    'The database information has been configured click <a href="install/index.php">here</a> to continue';
    fclose($Handle);
    ?>

  • #3
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    Actually, I was particularly hoping to be able to write to XML. Your script worked--I was able to write & edit a text file. But I really need to open, edit, and read xml.

    I probably should have said that in the beginning . . . . my bad!

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Yes, you should have. Look into SimpleXML.

  • #5
    Regular Coder
    Join Date
    Sep 2007
    Location
    AZ, USA
    Posts
    685
    Thanks
    6
    Thanked 46 Times in 46 Posts
    thank you inigoesdr, thats EXACTLY what im looking for!!! My only problem now is that my server has PHP 4.4.6 (and no mySQL) and SimpleXML is a PHP5 thing. Is there anything else I can look into? Or do I just need to upgrade my hosting plan?

    One last thing, and this might sound like an idiot question. I'm new to php, bear with me. Is it possible to upgrade the php version on the server for free? And can the client do it (i.e. can I)?

    Edit: I just found simpleXML44, a simpleXML thing for PHP4. I downloaded and included it on my server with these files:

    php file:
    Code:
     <?php
     require_once('class/IsterXmlSimpleXMLImpl.php');
     
     // read and write a document
     $impl = new IsterXmlSimpleXMLImpl;
     $doc  = $impl->load_file('simple.xml');
     print $doc->asXML();
     // output is the file as given above
     
     // access a node's CDATA
     print $doc->root->node->child[0]->CDATA();
     print "\n";
     // output is "Tom Foo"
     
     // access attributes
     $attr = $doc->root->node->child[1]->attributes();
     print $attr['gender'];
     print "\n";
     // output is "f"
     
     // access children
     foreach( $doc->root->node->children() as $child ) {
        print $child->CDATA();
        print "\n";
     }
     // output is: 
     // Tom Foo 
     // Tamara Bar
     
     // change or add CDATA
     $doc->root->node->child[0]->setCDATA('Jane Foo');
     print $doc->asXML();
     
     // change or add attribute
     $doc->root->node->child[0]->setAttribute('gender', 'f');
     print $doc->asXML();
     
     ?>
    Xml file
    Code:
     <?xml version="1.0" encoding="utf-8" standalone="yes"?>
     <root>
        <node>
          <child gender="m">Tom Foo</child>
          <child gender="f">Tamara Bar</child>
        </node>
     </root>
    but I keep geting this error message:

    WARNING isterxmlexpatnonvalid->parse(): expat: xml declaration not at start of external entity [1/1/1]

    Fatal error: Call to a member function on a non-object in /home/nathan/public_html/44.php on line 11
    Last edited by binaryWeapon; 11-04-2007 at 02:32 PM. Reason: found simpleXML44

  • #6
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Quote Originally Posted by binaryWeapon View Post
    My only problem now is that my server has PHP 4.4.6 (and no mySQL) and SimpleXML is a PHP5 thing. Is there anything else I can look into? Or do I just need to upgrade my hosting plan?
    You should look into a host that has PHP5. PHP4's end-of-life is in less than two months.
    Quote Originally Posted by binaryWeapon View Post
    One last thing, and this might sound like an idiot question. I'm new to php, bear with me. Is it possible to upgrade the php version on the server for free? And can the client do it (i.e. can I)?
    Not possible unless you own or have full access to the server. You should talk to your host though, they might have something in place already or they might be planning to upgrade soon.


  •  

    Posting Permissions

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