I am making a website where the user needs to submit their name and last name into the form and then I need it to create a text file with the results of whatever the user put in and store it on the server. The location I want it saved is in a folder at /var/www/html/text/fields.txt This is my code I want to run. 1.HTML(sendmessage.html) 2. PHP- (Action-page.php)
<div class="messageform">
<form action="action_page.php" method="post">
First name:<br>
<input type="text" name="firstname" value=""><br>
Last name:<br>
<input type="text" name="lastname" value=""><br><br>
<input type="submit" value="Submit">
</form>
<?php
if( isset($_POST['firstname'] ) && isset( $_POST['lastname'] ) ) {
// Manage our constants easier up top in one place
$TextFilePath = './var/www/html/text';
$FileName = 'fields.txt';
$FullPathAndFileName = "$TextFilePath$FileName";
// Create our data to write
$txt = $_POST['firstname'].' - '.$_POST['lastname'] . PHP_EOL;
// Validate dir exists, if not, create it
if (!is_dir($TextFilePath))
mkdir($TextFilePath, 0755, true);
// Open/create our file to write to
$file = fopen($FullPathAndFileName, "w") or die("Can't find or create $FileName");
// Write out the form data
fwrite($file, $txt);
// Close the file
fclose($file);
}
Note that the HTML one is just the form part of the code, not the whole thing.
The code works in vs studio but whenever I run it on apache(raspberry pi) it seems to work but when I look at the files it never is made.
Any help is appreciated and just to let you know I have tried some guides online.