Matt Shaffer Matt Shaffer - 6 months ago 10
HTML Question

Can you use HTML to upload a text input as a file?

I am trying to take a user's input from a HTML text form

<input type="text" name="comments" value="Comment">


is there a way to submit that input text as a txt file when the user clicks on the submit button for the form like if I had a

type='file'


type of form?

(I can use native html, js, or php in this project)

Answer

firstly, make a directory with name temp, then put .htaccess file in it, which contains: deny from all. Then make a php-script with code:

<?php
if (!empty($_POST)) {
    file_put_contents('temp/file.txt', $_POST['comments']);
}

don't forget about write permission for temp folder!

BUT! Never save files with .php extention, coz some true-hackers can write a php code in textarea and then save, and execute it (if you don't create .htaccess with deny from all). Also, you can deny executing of all files in your temp/ directory using chmod (only for linux, for windows .htaccess will help you). Also you can put your temp/ directory under the www/ (webroot) directory of your server (the path to you www/ directory is something like this: /site.com/www/, just make a dir with path: /site.com/temp/), then nobody will can to execute created files. Writing a file to this folder will be like this:

file_put_contents('../temp/file.txt', $_POST['comments']);