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
https://forums.phpfreaks.com/topic/26534-writing-form-data-to-a-file-syntax-help/
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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.