Can i Write to a file with form in php?

Can i Write to a file with form in php?
0.0 0

#1

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>

#2

Where’s the code for the form, please?


#3

Please explain in detail what you are trying to accomplish. Also, provide all relevant code (HTML, PHP, JavaScript) for your project. Be very specific with what code you think should do something in particular and then what results you are seeing (error messages, etc…).


#4

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>

#6

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>

#7

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);

#8

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