0

I am trying to add some text from my html form with 4 inputs. I may have been staring myself blind at this, but I can't figure out where it fails.

Ihave tried with "a" instead of doing the "r" and then "w" option.

$fuldenavn = $_POST["navn"];
$voksneantal = $_POST["voksne"];
$bornantal = $_POST["born"];
$kontaktinfo = $_POST["contacts"];
$content = $fuldenavn. PHP_EOL .$voksneantal. PHP_EOL .$bornantal. PHP_EOL .$kontaktinfo.;
$file = fopen("tilmeldinger.txt", "r");
$old_content = fread($file, filesize('tilmeldinger.txt'));
fclose($file);
$file = fopen('tilmeldinger.txt', 'w');
fwrite($file, $content."\n".$old_content);
fclose($file);
echo "Din tilmelding er modtaget. Tak! :)";

I have had two different outcomes, either blank page or error 500.

html form:

<form method="post" action="tilmeld_noc.php">

Fulde navn: <input type="text" name="navn"></tab>
Antal Voksne: <input type="text" name="voksne" value="0">
Antal u. 12: <input type="text" name="born" value="0">
E-mail el. tlf.: <input type="text" name="contacts" value="0"><br>
<input type="submit" id='tilmeldbutton' value="Send Tilmelding" />
</form>
Sims
  • 3
  • 1
  • 1
    File ownership/permissions? If you're writing to it with PHP from a web form (which you appear to be doing) it would need to be writable by Apache (assuming mod_php) - so needs to be writable by `www-data` (assuming that's the Apache user)... could really use a bit more info. – CD001 Sep 12 '19 at 13:57
  • What have you tried to debug that problem? It should not be too hard to start some simple debugging attempts using vardump, or even using a debugger like XDebug. Finally, have you checked for errors occuring on runtime? – Nico Haase Sep 12 '19 at 13:58
  • If it's a 500 error, then you'll want to check the server error logs to find out what exactly the error is. – aynber Sep 12 '19 at 13:59
  • 1
    _“I have had two different outcomes, either blank page or error 500.”_ - in both cases, checking the error logs is the absolute first thing you should do. (Before you even ask about your problem anywhere.) – misorude Sep 12 '19 at 14:00
  • 1
    There is a dot after $kontaktinfo that would cause a syntax error. – René Beneš Sep 12 '19 at 14:01
  • Check your `error_log`, paste the actual errors – delboy1978uk Sep 12 '19 at 14:01
  • well spotted Rene – delboy1978uk Sep 12 '19 at 14:01
  • I'm running IIS 8. After removing the syntax error, I am now at that it echoes correctly but nothing is written to the file, the file (and folder) has both DefaultApplicationPool and MACHINE_NAME\IIS_IUSRS added - and another txt file is being written to just fine in the folder above this one. – Sims Sep 12 '19 at 21:38

2 Answers2

0

For Error 500 you can turn on ini_set('display_errors', 1) and check $content = $fuldenavn. PHP_EOL .$voksneantal. PHP_EOL .$bornantal. PHP_EOL .$kontaktinfo.; there is "." at the end.

DuhVir
  • 447
  • 1
  • 4
  • 15
0

This can have a number of reasons:

Permissions Problem

Check if the webserver has rights to write to the specific file. If you are using linux, this means the directory the file will reside in has to be writeable by the webserver user (with apache usually called www-data) and the file itself needs to be writeable by the webserver.

For a simple test, create the file yourself and execute chmod a+rw <filename> to give everyone access. (This is only for testing, just quickly read up on linux permissions and you'll figure it out quickly).

No disk space left

Pretty obvious, make up some space.

Opening correctly

If the file doesn't exist yet, you'll need permissions to the directory and open the file with w+. (Create file and write)

Others

Enable all warnings in php, and check if the output of fopen, fwrite or fclose is false. Usually warnings are printed which explain the reason of failure.

See: https://stackoverflow.com/a/42155639/2232127

JensV
  • 3,997
  • 2
  • 19
  • 43
  • IUSR needed access to the folder, funny enough that worked without IUSR on a different folder. – Sims Sep 12 '19 at 22:00