'PHP File Write - Error Message Every Time
So here's my code keep hitting my error message, have fiddled with permissions but no luck, any ideas?
<?php
//create variables
$first_name = (int) $_POST['first_name'];
$last_name = (int) $_POST['last_name'];
$document_root = $_SERVER['/var/www'];
$date = date('H:i, jS F Y');
?>
<DOCTYPE html>
<html>
<head>
<title>We get the message...</title>
</head>
<body>
<h1>We get the message . . .</h1>
<?php
$output_string = $date."\t".$first_name.$last_name." \t".$message."\n";
@$fp = fopen("$document_root-messages.txt", 'w+');
if (!$fp) {
echo "<p>apologies</p>";
exit;
}
flock($fp, LOCK_EX);
fwrite($fp, $output_string, strlen($output_string));
flock($fp, LOCK_UN);
fflush($fp);
fclose($fp);
echo "<p>message written.</p>";
?>
</body>
</html>
Solution 1:[1]
@$fp = fopen("$document_root-messages.txt", 'w+');
You didn't actually make a path, there's no such file as:
/var/www-messages.txt
This is now a real path:
@$fp = fopen("$document_root/messages.txt", 'w+');
And is I assume what you meant, assuming the file in /var/www is message.txt.
The comments about the bad practice of making /var/www writable are also correct, it's best when learning to start out with good practices, since if you start with bad ones, you just have to unlearn them anyway, and that's more work.
The way to avoid this is to just get used to being more clear, like so:
$file_path = $document_root . '/messages.txt';
echo $file_path . '<br>';
@$fp = fopen($file_path, 'w+');
Then you get rid of the echo after it's working, the echo is also good advice. If you get used to cramming all the actions inside of something, it's very hard to debug, if you construct your strings outside of say fopen, then you can debug the path by echoing it, and you don't get all confused. Notice how clear that is, and how easy to debug?
Solution 2:[2]
First of all, I would map the array into an object (this is not needed, it just feels cleaner this way).
values = Object.fromEntries(a.map(entry => [entry.key, entry.value]))
Then I believe, all you really need is to find all entries that have non-negative "value" (if that is not the case, please comment below and I will correct this answer).
usedFields = Object.entries(values).filter(entry => entry[1])
Solution 3:[3]
If you want only to get the difference, and that is what i can read from question title, shouldn't this work?
const initialFormData = [{key: 'USER_DEFINED_QUESTIONS', value: ''}, {key: 'RECOMMENDED_FIELDS', value: ''}];
// when a value has been entered
const values = [{key: 'USER_DEFINED_QUESTIONS', value: 'test'}, {key: 'RECOMMENDED_FIELDS', value: ''}];
const keysWhichDiffer = values.map(x => initialFormData.find(y=> x.key===y.key && x.value!==y.value)?.key);
const result = keysWhichDiffer.filter(x=>x);
console.log('res', result)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | gavlna |
| Solution 3 |
