I am trying to get the file permissions to the executable to save the responses from the forms using PHP. Unfortunately, I am unable to do it. Whenever I click CHMOD, nothing happens.
I want to execute PHP but I couldn’t get that. This is the code:
<?php
header('Content-Type: text/plain');
// Set the file path where you want to store the form data
$dataFilePath = __DIR__ . 'data/form_data.txt';
// Check if the form is submitted
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get form data
$name = isset($_POST['name']) ? $_POST['name'] : '';
$email = isset($_POST['email']) ? $_POST['email'] : '';
$phone = isset($_POST['phone']) ? $_POST['phone'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
// Validate data (you may want to add more validation)
if (empty($name) || empty($email) || empty($phone) || empty($message)) {
die('All fields are required.');
echo $name;
echo $email;
echo $phone;
echo $message
}
// Create a formatted string with form data
$formData = "Name: $name\nEmail: $email\nPhone: $phone\nMessage: $message\n\n";
// Append data to the file
$result = file_put_contents($dataFilePath, $formData, FILE_APPEND);
// Check if the file operation was successful
if ($result === false) {
die('Error: Could not write to the file.');
}
// Display success message
echo 'Form submission successful!';
} else {
// Display error message if accessed directly
echo 'Error: Invalid form submission.';
}
?>
Now, it should be storing the data from the forms in data/form_data.txt but it doesn’t.
Can you help me out? ( I used ChatGPT in every single way. It doesn’t work )
This part is incorrect. The __DIR__ parameter does not have a trailing slash. So if the __DIR__ is htdocs, then you’re now trying to write data to the file htdocsdata/form_data.txt. Adding a slash, so it becomes /data/form_data.txt, should do the trick.