Computer Hope

Internet & Networking => Web design => Topic started by: zask on March 30, 2016, 06:32:08 PM

Title: Is it possible to write to a text file in html
Post by: zask on March 30, 2016, 06:32:08 PM
Hello, I was wondering if it is possible to create a text file with text in it from html, and if so, is it possible to specify a name for that text file as well?

I've tried using this code but it doesn't work and i dont know the proper way to create a htaccess file because i've never used php before, so i need help with doing that.

    <html>

   <head>
   <title>Writing to a text file</title>
   </head>
   <body>

   <?php

   // Open the text file
   $f = fopen("textfile.txt", "w");

   // Write text line
   fwrite($f, "PHP is fun!");

   // Close the text file
   fclose($f);

   // Open file for reading, and read the line
   $f = fopen("textfile.txt", "r");
   echo fgets($f);

   fclose($f);

   ?>

   </body>
   </html>

Lastly can someone explain what this code is doing specifically? Thank  you.
Title: Re: Is it possible to write to a text file in html
Post by: BC_Programmer on March 30, 2016, 06:54:26 PM
You can't have php code in a html file. PHP is run on the server side, so it must be run on a server.
Title: Re: Is it possible to write to a text file in html
Post by: zask on March 30, 2016, 07:13:45 PM
but iv'e seen this done before, maybe im not saying correctly what i want.

im looking for something like in this video, it explains what i mean.

https://www.youtube.com/watch?v=ka5m_kvQUFo
Title: Re: Is it possible to write to a text file in html
Post by: BC_Programmer on March 30, 2016, 07:21:41 PM
Oh. I see. You have a web server. you don't want to write to a text file in HTML; you want to write to a text file in PHP, and you want the apache server to run .html files through mod_php.

If you can't change .htaccess you can change your server's configuration. For apache it is /etc/apache2/mods-enabled/php5.conf on *nix though I'm not sure where it would be on windows.
You can add this to the file:

Code: [Select]
<FilesMatch ".+\.html$">
    SetHandler application/x-httpd-php
</FilesMatch>
Title: Re: Is it possible to write to a text file in html
Post by: zask on March 30, 2016, 07:34:27 PM
Exactly, Do you know specifically how i can do this? like for example, where specifically do i do what on the web server?
Or do you know anywhere where i can have a good start in trying to learn to
do this?
Title: Re: Is it possible to write to a text file in html
Post by: BC_Programmer on March 30, 2016, 07:46:24 PM
Exactly, Do you know specifically how i can do this? like for example, where specifically do i do what on the web server?
Or do you know anywhere where i can have a good start in trying to learn to
do this?

If you have access to the web server you should be able to create a .htaccess file (leading period) in much the same way you would create other files. At which point I'd expect that the video you linked describes what you need to add to have it process .html as .php files.
Title: Re: Is it possible to write to a text file in html
Post by: zask on April 02, 2016, 11:59:45 AM
If you have access to the web server you should be able to create a .htaccess file (leading period) in much the same way you would create other files. At which point I'd expect that the video you linked describes what you need to add to have it process .html as .php files.

Yes thats correct