Jump to content

Writing form data to a file (syntax help)?


cmazur

Recommended Posts

All I need to do is write the data entered on form of a webpage to a text file.
I think I have the write syntax here, but the error part of my fopen() keeps executing.
If I'm not mistaking, the second argument of my fopen(), 'a+', should create the file
if it doesn't already exist; which, in this case, the file doesn't exist

Here's the code, anything that you see goin' wrong here would be helpful.
Thanks again...

[code]


<?php
//Get form data values
$title = $_POST["title"];
$composer = $_POST["composer"];
$performer = $_POST["performer"];

//Generate data to be written to the data file
$data = "Song:  " + $title + "\n" +
"Composer:  " + $composer + "\n" +
"Artist/Group:  " + $performer + "\n\n\n";

//Open a file and write the results to it
$file = fopen("songs.dat", "a+")
or die ("ERROR - songs.dat cannot be opened");

$bytes_written = fwrite($file, $data);

fclose($file);
?>
[/code]
Link to comment
Share on other sites

If your writing this from a webpage, make sure that the user Apache runs under has write permissions on songs.dat.

In your shell, type $chown apache.apache songs.dat
As for the code itself...

Try..

[code]               
                <?php
                //Get form data values
                        $title = "Test";
                        $composer = "Test2";
                        $performer = "Test3";

                //Generate data to be written to the data file
                $data = "Song:  " . $title . "\n" . "Composer:  " . $composer ."\n" . "Artist/Group:  " . $performer . "\n\n\n";

                //Open a file and write the results to it
                $file = fopen("songs.dat", "a+")
                                or die ("ERROR - songs.dat cannot be opened");

                $bytes_written = fwrite($file, $data);

                fclose($file);
                ?>
[/code]

Substituting (.) for (+)

Rab
Link to comment
Share on other sites

Well, changing the + to . didn't do anything for me.
As far as write access is concerned, that is probably the problem.
I'm using a school server. Both the XHTML and PHP files are located in my directory
to which I have access to write and read etc.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.