我目前正在开发一个创建 xml 文件的 PHP 脚本。当我在浏览器中运行脚本时,它不会被写出。当我使用 shell 命令“php /filelocation”时,它工作得很好。为什么是这样?
<?php
include('parse.php');
$test;
$test[0] = "Name:Actinium";
$test[1] = "Symbol:Ac";
$test[2] = "Atomic number:89";
$test[3] = "Atomic weight:[227]";
xml_write($test);
?>
<?php
function xml_write($s)
{
$temp = explode(":",$s[0]);
$data;
$count = 0;
for($i = 0;$i< sizeof($s);$i++)
{
$temp = explode(":",$s[$i]);
$data[$count] = $temp[1];
$count++;
}
//creates the root element
$xml = new DOMDocument("1.0");
$root = $xml->createElement("table");
$xml->appendChild($root);
//creates name element
$name = $xml->createElement("name");
$nameText = $xml->createTextNode($data[0]);
$name->appendChild($nameText);
//creates symbol element
$symbol = $xml->createElement("symbol");
$symbolText= $xml->createTextNode($data[1]);
$symbol->appendChild($symbolText);
//creates atomic number element
$number = $xml->createElement("number");
$numberText = $xml->createTextNode($data[2]);
$number->appendChild($numberText);
//creates atomic mass element
$mass = $xml->createElement("mass");
$massText = $xml->createTextNode($data[3]);
$mass->appendChild($massText);
//creates "element" element and binds the properties to it
$ele = $xml->createElement("element");
$ele->appendChild($name);
$ele->appendChild($symbol);
$ele->appendChild($number);
$ele->appendChild($mass);
//attaches ele to root
$root->appendChild($ele);
$xml->formatOutput = true;
echo "<xmp>".$xml->saveXML()."</xmp>";
$xml->save("/home/anthony/database/table.xml") or die("Error");
}