My class method insert_new() is supposed to create and populate a
brand new XML file.
However, the file exists but its contents are never placed into the
newly-created file, the file always remains empty (byte size 0).
[PHP]
/**
* Class HGFContentXMLWriter is a special extension of XMLWriter class
to handle HGF's special
* need for insertion of XML content per section
*
* @author Phil Powell
* @version 1.0.0
* @package HGF
*/
class HGFContentXMLWriter extends XMLWriter {
/**
* @access private
* @var mixed $content XML content
*/
var $content;
/**
* Constructor
*
* @access public
* @param mixed $indent (optional ' ')
*/
function HGFContentXMLWriter($indent = ' ') { // CONSTRUCTOR
parent::XMLWriter($indent); // "super()"
$this->setFormattedContent();
}
//------------------------ --* GETTER/SETTER METHODS *--
-----------------------------
/**
* Retrieve formatted content
*
* @access private
* @return mixed XML
*/
function &getFormattedContent() {
return $this->content;
}
/**
* Set formatted content
*
* @access private
*/
function &setFormattedContent() {
foreach ($_REQUEST as $key => $val) if (!isset(${$key}))
${$key} = $val;
$this->content = '<entry date="' . date('D M j H:i:s \G\M\TO
Y') . "\">\n" .
' <title>[!CDATA{' . $title . "}]</title>\n"
..
' <body>[!CDATA{' . $body . "}]</body>\n" .
'</entry>';
}
//--------------------- --* END OF GETTER/SETTER METHODS *--
-------------------------
/**
* Insert into existing XML content found in session variable
*
* @access public
*/
function &insert() { // STATIC VOID METHOD
global $userPath, $prefix;
$this->setFormattedContent();
foreach ($_REQUEST as $key => $val) if (!isset(${$key})) ${$key} =
$val;
$content = $_SESSION["${prefix}_content"]; // RETRIEVE FROM
SESSION
$content = preg_replace('/^(' .
str_replace('?', '\\?' , str_replace('/', '\\/', $this->xml)) .
'[\n\r\s\t]*[\s\t]*<' . $section . '>[\n\r\s\t]*)/i',
'$1' . $this->getFormattedContent() . "\n",
$content);
if ($preview) {
// SAVE TO PREVIEW XML FILE
$fileID = @fopen("$userPath/admin/xml/${section}.xml", 'w');
} elseif ($save) {
// SAVE TO LIVE XML FILE
$fileID = @fopen("$userPath/xml/${section}.xml", 'w');
}
if ($fileID) {
@fputs($fileID, $content);
@fflush($fileID);
@fclose($fileID);
if ($save) {
$_SESSION["${prefix}_content"] = '';
session_unregister("${prefix}_content");
}
}
}
/**
* Insert into new XML file with prepared content
*
* @access public
*/
function &insert_new() { // STATIC VOID METHOD
global $userPath, $prefix;
$this->setFormattedContent();
foreach ($_REQUEST as $key => $val) if (!isset(${$key})) ${$key} =
$val;
if ($preview) {
// SAVE TO PREVIEW XML FILE
$fileID = @fopen("$userPath/admin/xml/${section}.xml", 'w');
} elseif ($save) {
// SAVE TO LIVE XML FILE
$fileID = @fopen("$userPath/xml/${section}.xml", 'w');
}
if ($fileID) {
$content = $this->xml . "<$section>\n" .
$this->getFormattedContent() . "\n</$section>";
print_r("fileID = $fileID and content =
$content<P>"); // PRINTS $fileID and $content EVERY TIME
fputs($fileID, $content, strlen($content));
fclose($fileID);
if ($save) {
$_SESSION["${prefix}_content"] = '';
session_unregister("${prefix}_content");
}
}
}
/**
* Update existing XML content found in session variable
*
* @access public
*/
function &update() { // STATIC VOID METHOD
global $userPath, $prefix;
$this->setFormattedContent();
}
}
[/PHP]
Environment: PHP 4.3.8 FreeBSD with allow_url_fopen to TRUE. No
warnings, errors, anything.
Thanx
Phil