Hi all

I'm creating XML with php's native DOMDocument functionality. When I try save the XML it doesn't appear on the server and I can't figure out why.

<?php
// Initiate the XML
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->formatOutput = true;

// Code that creates XML elements and child elements

// Save the XML
echo $doc->saveXML();
$doc->save("file.xml");
?>

The above should work but doesn't. I've made sure that DOM is enabled which it is.

Anyone have any ideas why this wouldn't be saving the XML to the server?

Dani AI

Generated

Short, practical troubleshooting and next steps that address the replies from and .

If the XML prints in the browser but no file appears, the usual causes are: the script is writing to a different directory than expected, the webserver/PHP user lacks write permission, PHP errors are suppressed, or the DOM extension is not actually available in the running PHP build (the compile flag you reported, --disable-dom, indicates that possibility). was right to check the return value of the save call; ’s php.ini observation points to a server-side build issue that the host must fix.

Quick tests to run (put this in a new PHP file and call it from the same location as the script):

<?php
ini_set('display_errors',1);
error_reporting(E_ALL);

echo "cwd: ".getcwd()."\n";
echo "__DIR__: ".__DIR__."\n";
var_dump(class_exists('DOMDocument'));   // checks DOM availability
var_dump(is_writable(__DIR__));          // checks write permission
@file_put_contents(__DIR__.'/perm-check.txt', "ok\n") or error_log('write failed: '.__DIR__);
?>

If class_exists('DOMDocument') is false, ask the host to enable the DOM extension (or install the distribution package that provides it). If the write test fails, check the script location and permissions with ls -la and the webserver user (www-data, apache, or nobody depending on the distro). Prefer changing ownership or group to the webserver account and using restrictive modes (for example chown/chgrp + 750/640) rather than 0777. Also review PHP/Apache error logs (tail the error log) and use absolute paths (eg. __DIR__ or full path under public_html) to avoid surprises. These steps resolve the vast majority of "save not creating file" cases.

Recommended Answers

All 4 Replies

>> $doc->save("file.xml");

What does this return? FALSE or a number?

>> $doc->save("file.xml");

What does this return? FALSE or a number?

If I remove the echo for the saveXML function then the page is blank. Nothing is returned.

Perhaps you should put some data in it? Also: output how many bytes were written

<?php
// Initiate the XML
$doc = new DOMDocument('1.0', 'UTF-8');
$doc->formatOutput = true;

$root = $doc->createElement('something');
$root = $doc->appendChild($root);


// Save the XML
echo $doc->saveXML();
echo 'Bytes written: '.$doc->save("file.xml");
?>

If this doesn't work and the output is not showing any bytes written, then you probably do not have the privileges to write the file. Are you using Linux or Windows?

Echoing the save function doesn't show anything. I've just had another look at the php.ini file (linux server) and I noticed that the dom section says it's enabled but the Configuration Command shows '--disable-dom'

I'm guessing I'll need to get hold of the hosting company and get them to enable it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.