...

View Full Version : Editing a text or xml file



binaryWeapon
11-03-2007, 10:41 PM
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

mathew edison
11-03-2007, 11:45 PM
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
$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);
?>

binaryWeapon
11-04-2007, 12:56 AM
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!

Inigoesdr
11-04-2007, 04:32 AM
Yes, you should have. Look into SimpleXML (http://php.net/simplexml).

binaryWeapon
11-04-2007, 02:04 PM
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)?

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

php file:

<?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

<?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

Inigoesdr
11-04-2007, 05:08 PM
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.

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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum