如何使用 PHP 在 Web 服务器上保存 XML 文件? [英] How to save an XML file on the web server using PHP?

查看:34
本文介绍了如何使用 PHP 在 Web 服务器上保存 XML 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 PHP 初学者,对 XML 操作一无所知.我正在处理如下所示的 Google CSE 注释 XML:

I am a beginner in PHP and I know nothing about XML manipulation. I am working on a Google CSE annotation XML shown below:

  <?xml version="1.0" encoding="UTF-8" ?> 
- <Annotations>
- <Annotation about="http://sanspace.in/">
  <Label name="_cse_byxamvbyjpc" /> 
  </Annotation>
- <Annotation about="http://blog.sanspace.in/">
  <Label name="_cse_byxamvbyjpc" /> 
  </Annotation>
- <Annotation about="http://google.com/">
  <Label name="_cse_exclude_byxamvbyjpc" /> 
  </Annotation>
  </Annotations>

我想从上面显示的文件中实现这一点:

I want achieve this from the above shown file:

  <?xml version="1.0" encoding="UTF-8" ?> 
- <Annotations>
- <Annotation about="http://sanspace.in/">
  <Label name="testString1" /> 
  </Annotation>
- <Annotation about="http://blog.sanspace.in/">
  <Label name="testString2" /> 
  </Annotation>
- <Annotation about="http://google.com/">
  <Label name="testString2" /> 
  </Annotation>
  </Annotations>

到目前为止,我已经尝试过:

So far, I have tried:

<?php
if (file_exists('test.xml'))
  {
  $xml = simplexml_load_file('test.xml');
  }
else
  {
  exit('Error.');
  }
foreach($xml->Annotation as $annotation)
    {
    if ($annotation["about"]=="http://sanspace.in/") 
        {  $annotation->Label["name"]="testString1";  } 
    else 
        {  $annotation->Label["name"]="testString2";  } } 

$dom = new DOMDocument('1.0'); 
$dom->preserveWhiteSpace = false; 
$dom->formatOutput = true; 
$dom->loadXML($xml->asXML()); 
echo $dom->saveXML();
$dom->save("test.xml");
?> 

此代码执行任务,但不会将其保存到文件中.

This code performs the task but it doesn't save it into the file.

我的问题是,$dom->save("test.xml"); 语句有什么问题?如何在服务器上保存 XML 文件?

My question is, what's wrong with the $dom->save("test.xml"); statement? How do I save the XML file on the server?

推荐答案

我看不出你的代码有什么问题.我认为您的权限有问题.

I don't see anything wrong with your code. I think you have a permission problem.

PHP 正在作为以下两个用户之一运行:

PHP is running as one of the following two users:

  • nobody 或者可能是 www-data(也就是网络服务器使用的系统用户)
  • 您的用户名
  • nobody or possibly www-data (aka the system user the web server uses)
  • Your user name

要使 PHP 以拥有 Web 根目录的用户(即您)的身份运行,必须为 PHP 启用 suexec.您无法写入具有 0644 权限的文件这一事实几乎说明事实并非如此.

For PHP to run as the user that owns the web root (i.e. you), suexec has to be enabled for PHP. The fact that you can't write to a file with 0644 permissions pretty much says that it is not.

您有两个选择:

  • 重新配置服务器,使 PHP 以拥有 Web 根目录的用户身份运行
  • 使文件世界可写

我强烈推荐第一个而不是第二个.但是,您并不总是有这样的选择.如果您的主机(或系统管理员,或其他任何人)不能或不会为 PHP 启用 suexec,则您必须授予文件 0777 权限,也就是 rwxrwxrwx.

I highly recommend the first over the second. However, you don't always have that choice. If your host (or sysadmin, or whoever) can't or won't enable suexec for PHP, you'll have to give the file 0777 permissions, aka rwxrwxrwx.

您可能想通过 ssh 登录并首先使用 touch 命令(或目录,如果这是您需要的通过 mkdir)创建输出文件,然后给它所需的权限.

You might want to login via ssh and create the output file using the touch command first (or directory, if that's what you need via mkdir), then give it the needed permissions.

这篇关于如何使用 PHP 在 Web 服务器上保存 XML 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆