473,387 Members | 1,624 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

fputs() never puts!

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
Jul 17 '05 #1
2 2833
Phil Powell wrote:
$fileID = @fopen("$userPath/xml/${section}.xml", 'w');
}

if ($fileID) {
@fputs($fileID, $content);
@fflush($fileID);
@fclose($fileID); ...SNIP.. Environment: PHP 4.3.8 FreeBSD with allow_url_fopen to TRUE. No
warnings, errors, anything.


No warnings etc because you've prefixed your calls with @.

Where is variable $section defined?
Jul 17 '05 #2
2metre <2m****@xxxhersham.net> wrote in message news:<cn**********@hercules.btinternet.com>...
Phil Powell wrote:
$fileID = @fopen("$userPath/xml/${section}.xml", 'w');
}

if ($fileID) {
@fputs($fileID, $content);
@fflush($fileID);
@fclose($fileID);

..SNIP..
Environment: PHP 4.3.8 FreeBSD with allow_url_fopen to TRUE. No
warnings, errors, anything.


No warnings etc because you've prefixed your calls with @.

Where is variable $section defined?


Oh sorry this was resolved it was due to the behavior of another
script altogether causing the written file to be overwritten with
NULL.

Thanx, and yes I know about '@' and $section is defined earlier as a
parse from $_REQUEST['section'].

Phil
Jul 17 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Michael T. Peterson | last post by:
I am trying to get the file referenced by the following url: http://waterdata.usgs.gov/wa/nwis/uv?dd_cd=01&dd_cd=02&format=rdb&period=1&site_no=12149000 I'm using parse_url to get the host,...
3
by: cdt sylvestre | last post by:
hi i have a form with a textarea on the action page i write its content with fopen and fputs but it s writing a dos file (i m on a linux server) thx all
145
by: Sidney Cadot | last post by:
Hi all, In a discussion with Tak-Shing Chan the question came up whether the as-if rule can cover I/O functions. Basically, he maintains it can, and I think it doesn't. Consider two...
1
by: Yoni Rabinovitch | last post by:
Hi, We have a large code base of "regular" C++ code (not MFC, COM or ATL). With VC6.0, we used to compile the "regular" C++ code as static libraries (.lib files), and then we would link the...
0
by: dan | last post by:
I am trying to write to a file using fputs() but when the disk is full, EOF is not returned. Despite this, errno is set to ENOSPC (28) as expected. Has anyone encountered this problem before, as I...
1
by: xoinki | last post by:
hi experts, I need a little help in debugging this code.. would u pleeze kindly help me? here this program sends a datagram every 10 seconds and on reception it cheks whether the source IP is...
27
by: Chad | last post by:
Why doesn't fputs() write the null byte when the null-terminted string is written to a stream? I don't see the reasoning behind this. Chad
5
by: matt | last post by:
This is a strange one but I've been stuck on it for days. Any help appreciated. THE PLAN: I've a database that I use a script to grab all the entries for a particular field. I then want to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.