Ok on my wirte_data page of my form im gettin these errors on both the fwrite and fclose. I dont know why but heres my code:

<?php

$Title = $POST["Title"];
$Today = $
POST["Today"];
$Comments = $POST["Comments"];
$Var = $
POST["Var"];
$fp = fopen( "$newfile", "ab" );
$content = "$Today - $Title";

switch ($Today) {
case '555':
$newfile = "newfile.htm";
case '444':
$newfile = "stmary.htm";
break;
}

fwrite( $fp, $content );

fclose($newfile);

echo "All done!";
?>

Any ideas on whats going wrong in my script.

    im gettin these errors on both the fwrite and fclose

    And what might these errors be?

      Warning: fwrite(): supplied argument is not a valid stream resource in C:\Inetpub\vhosts\rctdloads.com\httpdocs\write_data.php on line 18

      Warning: fclose(): supplied argument is not a valid stream resource in C:\Inetpub\vhosts\rctdloads.com\httpdocs\write_data.php on line 20
      All done!

      Thats the error

        Basically, fopen() failed, possibly due to an invalid filename.

          And that may be because (judging from the given code), $newfile isn't being set until after $newfile is opened.

            Wp, i don't see that, i though I defined it first. The im defining $newfile in the switch statement then in fwrite im using the $fp which opens the new file. Even if that didnt work this script:

            <?php

            $Title = $POST["Title"];
            $Today = $
            POST["Today"];
            $Comments = $POST["Comments"];
            $Var = $
            POST["Var"];
            $content = "$Today - $Title";

            switch ($Today) {
            case '555':
            $newfile = "newfile.htm";
            case '444':
            $newfile = "stmary.htm";
            break;
            }

            fopen($newfile, ab);

            fwrite( $fp, $content );

            fclose($newfile);

            echo "All done!";
            ?>

            Is opening new file after it is defined and im still getting the same errors.

              brbsta wrote:

              Wp, i don't see that, i though I defined it first. The im defining $newfile in the switch statement then in fwrite im using the $fp which opens the new file. Even if that didnt work this script:

              Is opening new file after it is defined and im still getting the same errors.

              I think you problem is with your fopen() script... your resouce mode does not make any sense... what I mean by that is the "ab" you have in your fopen fuction - fopen($newfile, ab) check the php manual and you can only use:

              'r' Open for reading only; place the file pointer at the beginning of the file.

              'r+' Open for reading and writing; place the file pointer at the beginning of the file.

              'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

              'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

              'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

              'a+' Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

              'x' Create and open for writing only; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call. This option is supported in PHP 4.3.2 and later, and only works for local files.

              'x+' Create and open for reading and writing; place the file pointer at the beginning of the file. If the file already exists, the fopen() call will fail by returning FALSE and generating an error of level E_WARNING. If the file does not exist, attempt to create it. This is equivalent to specifying O_EXCL|O_CREAT flags for the underlying open(2) system call.

              so i think if you change your fopen($newfile, ab) to $fp = fopen($newfile, "a+). Which the a+ means if file does not exist create it. It might work. But I see several mistakes in your code.. Lets take a look at the end of the code, lets start at your file open script...

              I would do this..

              //I would set $fp = fopen($newfile, "a+");
              fopen($newfile, ab); //this is your code.

              //Then I would set fwrite($fp, $content);
              fwrite( $fp, $content ); //this is your code

              //The is would close the file fclose($fp);
              fclose($newfile); //this is your code

              do you see your mistakes

              echo "All done!";

                The 'b' means that the file is to be treated as binary - no messing around with linebreaks.

                fopen($newfile, ab);
                
                fwrite( $fp, $content );
                
                fclose($newfile);
                

                You're forgetting to put the filehandle returned by fopen() into $fp. And you want to fclose($fp), not $newfile.

                  Write a Reply...