views:

79

answers:

2

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/"&gt;
  <Label name="_cse_byxamvbyjpc" /> 
  </Annotation>
- <Annotation about="http://blog.sanspace.in/"&gt;
  <Label name="_cse_byxamvbyjpc" /> 
  </Annotation>
- <Annotation about="http://google.com/"&gt;
  <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/"&gt;
  <Label name="testString1" /> 
  </Annotation>
- <Annotation about="http://blog.sanspace.in/"&gt;
  <Label name="testString2" /> 
  </Annotation>
- <Annotation about="http://google.com/"&gt;
  <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. The input, output are available at the below links:

My xml: http://www.sanspace.in/tools/searchit/test.xml My php: http://www.sanspace.in/tools/searchit/test.php (contains the above code)

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

+2  A: 

There is nothing wrong with $dom->save("test.xml");.
You dont need to do the save with DOM though. Could just as well do it with SimpleXML:

$xml->saveXML("test.xml");

Make sure the destination folder is writable. Use an absolute path to make sure you are actually looking for the file in the right location.

Gordon
I tried that. But, no effect. The XML permission is `rw-r--r--`. Is that fine?
San
@San if the webserver is the owner of the file, yes. If not set the group permissions to `w` too. If the webserver is neither owner, nor in the file owner's group, `chmod` the owner to the webserver.
Gordon
Gordon, I tried the first and it didn't work out. I don't know how to change the owner. As I understand, the problem is not with the code. It's just my lack of knowledge how to edit the file on a server. I really appreciate your continuous support.
San
@San there should be errors if the file is not writable though. I am pretty sure you're just overlooking something.
Gordon
Gordon, at last I got it by changing file permissions to 777. Thanks for the continuous support. It was really very very helpful in each stage.
San
@San you should **never** set any publicly available resource to 777. This makes it world-writable. I've deliberatly left out this permission setting in favor of changing the owner.
Gordon
@Gordon But, I don't have any other way. There is only one user on this server. How do I change the owner?
San
@San if this is a *nix server you have to use [`chmod`](http://linux.die.net/man/1/chmod). Your webserver has a user and a group. One way to find out which it is, is to create a file with PHP.
Gordon
+1  A: 

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

PHP is running as one of the following two users:

  • nobody or possibly www-data (aka the system user the web server uses)
  • Your user name

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.

You have two options:

  • Re-configure the server so PHP runs as the user that owns the web root
  • Make the file world write-able

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.

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.

Tim Post
Tim, I don't know how to configure suexec. Will try for it. Is it ok if I have 777? Does it mean any lack of security?
San
It worked, Tim. Thanks a ton. :-)
San