Creating a flat text file using PHP and an HTML form


I am learning PHP because I need to create a flat text file using information contained in a form on a web site.

Example - User visits the web site, inputs information into several form fields, and presses submit. A text file is then created that contains the information. This text file is then forwarded on to a business that will load the text file into a software program.

I need a total of five fields. Four of the fields have text in them. The last field, which is required to be in the text file, contains 2192 spaces. Yes you read that correctly. How do I do this with PHP?


You could use a csv format for the file. See fputcsv() for a robust way to do it.

Will CSV place field separators in the text file? I can’t have any separators in the file, so no commas or such. If your fields were first name, last name, and date of birth for example, this is what I need in the text file:


file_put_contents() is way flexible I’d say and can handle any file format, including 2192 spaces
But OP wants complete code, not some hints, I suppose :slight_smile:

file_put_contents(implode(“”,$_POST).str_repeat(" ",2192),“text.txt”);

Thanks everyone for responding. I was able to accomplish my goal with the repeat string posted above!