Skip to content

Instantly share code, notes, and snippets.

@kenbarbour
Created March 22, 2021 23:20
Show Gist options
  • Save kenbarbour/c15ff43d259b9c8e3034bb367f1e273d to your computer and use it in GitHub Desktop.
Save kenbarbour/c15ff43d259b9c8e3034bb367f1e273d to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title>Write to file</title>
</head>
<body>
<?php
if (isset($_POST['userdata'])) {
$userdata = trim($_POST['userdata']);
$error = false; // will set to true we find an error
if (empty($userdata)) {
$error = "Please enter some data";
}
$filepath = __DIR__.'/data/userdata.txt';
if (!file_exists(dirname($filepath))) {
mkdir(dirname($filepath), 0777, true);
}
if (!$fh = fopen($filepath, 'a')) {
$error = "Error opening file";
}
if (!$error) {
fwrite($fh, $userdata."\n");
fclose($fh);
echo "<div>Data entered successfully</div>";
}
if ($error) {
echo "<div>Error: {$error}</div>";
}
}
?>
<form method="post">
<input type="text" name="userdata" placeholder="Enter your data" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment