Why does PHP think it's writing to a file but it isn't? Why does PHP think it's writing to a file but it isn't? php php

Why does PHP think it's writing to a file but it isn't?


You may use the PHP function file_put_contents which takes a filename and data you wish to write and writes it to a file. In your case, this could be some form of the HTML POST data sent. In the solution below I have encoded the values in JSON as it is a good representation of key/value pairs associated with forms like this. This could be done in any other form that suits your needs.

I have chosen to write the file to the same folder as the PHP file containing the form. This is not realistic in a production environment however, it shows that the PHP works saving the file.

If the location of the file is the problem then permissions is where you should be looking as that should be the only thing causing it to not write when using the below method. I have entered values in the form fields for easier testing but the placeholders should stay in a production environment and the values removed.

In any environment there should be both client and server-side validation taking place. I have removed the validation for simplicity and that it is off-topic but each value should be tested and filtered prior to anything else being done with it. Anyway, here is the working example combined into one file:

<?php/** * Check for submission of form. */$nl="<br>";if (isset($_POST['memory']) && isset($_POST['desc']) && isset($_POST['comm']) && isset($_POST['reset'])) {    try {        $data = json_encode($_POST, JSON_PRETTY_PRINT);        $file = "data.txt";        if ( file_put_contents ($file, $data) ) {            $msg = "File successfully saved.";        } else {            $msg = "Error saving file";        }                } catch (Exception $e) {        $msg = "Error saving file:" .$nl.$nl . $e->getMessage() . $nl.$nl . $e-getTrace();    }}?><!doctype html><html lang="en_US"><head><meta charset='utf-8'><title>Example: Save File</title></head><body><?php if (isset($msg)){ echo '< style="color:#100">'.$msg.'</p>'; } ?><form name="form1" method="post" enctype="multipart/form-data"><input type="text" placeholder="memory" name="memory" value="mem"><input type="text" placeholder="desc"   name="desc" value="something"><input type="text" placeholder="comm"   name="comm" value="more data"><input type="text" placeholder="reset"  name="reset" value="reset"><input type="Submit" Name = "Submit1" value="Create"></form></body></html>

Furthermore, submitting a form you should have the enctype attribute set on the form tag. If you are uploading a file <input type="file"> then it must be set to multipart/form-data. This allows for the binary sending of data via the HTTP Post.

<form enctype="multipart/form-data" action="..." id="....">

Additionally, as MDN points out in its documentation, you may add an attribute to the <input> or button elements.:

"... or n the formenctype attribute of the <input> or <button> elements."

In all cases you should be specifying the enctype of the form including during asynchronous requests using XMLHttpRequest.