php写文件问题

时间:2013-08-07 19:26:10

标签: php xml

我目前正在开发一个创建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");
}

2 个答案:

答案 0 :(得分:0)

如果您没有通过header()设置内容类型的'application / xml'或类似内容,那么浏览器正在尝试将xml呈现为html ...它将显示为空白页面。如果您从浏览器“查看源代码”,您应该能够看到一些输出。

或者您可以设置内容类型标头,浏览器会将其显示为XML。

修改

重新阅读后,我可能会误解。我以为你只是echo数据输出。我必须同意上述评论者,因为没有对该文件的许可,所以apache进程正在运行。

要排除权限,您可以(取决于主机)将路径设置为/tmp/table.xml并查看其是否有效。假设权限问题,那么apache用户(www-data可能?)将需要对该文件的rw权限。如果apache要创建一个新文件,它也需要父目录的rw权限。

答案 1 :(得分:0)

我之前遇到过类似的问题。我最好的猜测是,r / w权限仅授予所有者而不是_www,这会导致shell命令工作,浏览器访问失败。如果可以,请尝试chmod 666并查看是否有效。

或者,您可以转到Apache的httpd.conf文件并将用户从_www更改为您的用户名。但我不建议将其作为永久解决方案,因为它会带来许多潜在的安全问题。