Can i Write to a file with form in php?

there is my example code and it doeasn’t wordking:**

this is code $myfile = fopen("quotes.php", "w");
$txt = $_POST['text'];
fwrite($myfile, $txt);
fclose($myfile);

my form:

<form action="program.php" method="post">
    Channel 8 Title:<br><input type="text" name="channel0Title" value="Channel 7"><br>
    <input type="submit" id ="submitButton" value="Submit">
</form>

Where’s the code for the form, please?

1 Like

there is my form:

<form action="program.php" method="post">
    Channel 8 Title:<br><input type="text" name="channel0Title" value="Channel 7"><br>
    <input type="submit" id ="submitButton" value="Submit">
</form>

there is my form:

<form action="program.php" method="post">
    Channel 8 Title:<br><input type="text" name="channel0Title" value="Channel 7"><br>
    <input type="submit" id ="submitButton" value="Submit">
</form>

You’d need to host your program.php in a server that runs PHP.
Then the directory/file should have write access priveleges.
And more importantly, saving unfiltered user input to your server without doing a lot of sanitizing/checking is so insecure, especially saving the user input directly into a php file on a server — this is just a vector for your server being hacked.

To answer your question,

$txt = $_POST['text'];   // it should be the name of your form variable
// given this code
// <input type="text" name="channel0Title" value="Channel 7">
// it should be 

$txt = $_POST['channel0Title']; 

Again, this is a bad idea:

$txt = $_POST['channel0Title'];
fwrite($myfile, $txt);
2 Likes

Thank you so much for your help, i would take a care with your recommand.