Created
March 22, 2021 23:20
-
-
Save kenbarbour/c15ff43d259b9c8e3034bb367f1e273d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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