fputs() never puts!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Phil Powell

    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 HGFContentXMLWr iter 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 HGFContentXMLWr iter extends XMLWriter {

    /**
    * @access private
    * @var mixed $content XML content
    */
    var $content;

    /**
    * Constructor
    *
    * @access public
    * @param mixed $indent (optional ' ')
    */
    function HGFContentXMLWr iter($indent = ' ') { // CONSTRUCTOR
    parent::XMLWrit er($indent); // "super()"
    $this->setFormattedCo ntent();
    }

    //------------------------ --* GETTER/SETTER METHODS *--
    -----------------------------

    /**
    * Retrieve formatted content
    *
    * @access private
    * @return mixed XML
    */
    function &getFormattedCo ntent() {
    return $this->content;
    }

    /**
    * Set formatted content
    *
    * @access private
    */
    function &setFormattedCo ntent() {
    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->setFormattedCo ntent();
    foreach ($_REQUEST as $key => $val) if (!isset(${$key} )) ${$key} =
    $val;
    $content = $_SESSION["${prefix}_cont ent"]; // 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->getFormattedCo ntent() . "\n",
    $content);
    if ($preview) {
    // SAVE TO PREVIEW XML FILE
    $fileID = @fopen("$userPa th/admin/xml/${section}.xml" , 'w');
    } elseif ($save) {
    // SAVE TO LIVE XML FILE
    $fileID = @fopen("$userPa th/xml/${section}.xml" , 'w');
    }

    if ($fileID) {
    @fputs($fileID, $content);
    @fflush($fileID );
    @fclose($fileID );
    if ($save) {
    $_SESSION["${prefix}_cont ent"] = '';
    session_unregis ter("${prefix}_ content");
    }
    }
    }

    /**
    * Insert into new XML file with prepared content
    *
    * @access public
    */
    function &insert_new( ) { // STATIC VOID METHOD
    global $userPath, $prefix;
    $this->setFormattedCo ntent();
    foreach ($_REQUEST as $key => $val) if (!isset(${$key} )) ${$key} =
    $val;
    if ($preview) {
    // SAVE TO PREVIEW XML FILE
    $fileID = @fopen("$userPa th/admin/xml/${section}.xml" , 'w');
    } elseif ($save) {
    // SAVE TO LIVE XML FILE
    $fileID = @fopen("$userPa th/xml/${section}.xml" , 'w');
    }
    if ($fileID) {
    $content = $this->xml . "<$section> \n" .
    $this->getFormattedCo ntent() . "\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}_cont ent"] = '';
    session_unregis ter("${prefix}_ content");
    }
    }
    }


    /**
    * Update existing XML content found in session variable
    *
    * @access public
    */
    function &update() { // STATIC VOID METHOD
    global $userPath, $prefix;
    $this->setFormattedCo ntent();
    }


    }
    [/PHP]

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

    Thanx
    Phil
  • 2metre

    #2
    Re: fputs() never puts!

    Phil Powell wrote:[color=blue]
    > $fileID = @fopen("$userPa th/xml/${section}.xml" , 'w');
    > }
    >
    > if ($fileID) {
    > @fputs($fileID, $content);
    > @fflush($fileID );
    > @fclose($fileID );[/color]
    ...SNIP..[color=blue]
    > Environment: PHP 4.3.8 FreeBSD with allow_url_fopen to TRUE. No
    > warnings, errors, anything.[/color]

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

    Where is variable $section defined?

    Comment

    • Phil Powell

      #3
      Re: fputs() never puts!

      2metre <2metre@xxxhers ham.net> wrote in message news:<cnj7p3$f9 d$1@hercules.bt internet.com>.. .[color=blue]
      > Phil Powell wrote:[color=green]
      > > $fileID = @fopen("$userPa th/xml/${section}.xml" , 'w');
      > > }
      > >
      > > if ($fileID) {
      > > @fputs($fileID, $content);
      > > @fflush($fileID );
      > > @fclose($fileID );[/color]
      > ..SNIP..[color=green]
      > > Environment: PHP 4.3.8 FreeBSD with allow_url_fopen to TRUE. No
      > > warnings, errors, anything.[/color]
      >
      > No warnings etc because you've prefixed your calls with @.
      >
      > Where is variable $section defined?[/color]

      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

      Comment

      Working...