Jump to content

- PHP within HTML writing to a .txt file -


ImranP

Recommended Posts

Hello all, I am very new to HTML and PHP coding so could you please bear with me.

 

All I am trying to do is write a string to a text file, and I have tried many examples. Alot of them seem to be similar to this code and it doesn't seem to want to work...

 


<HTML>
<HEAD>

<SCRIPT language="JavaScript">

function WriteFile()
{
   var fso  = new ActiveXObject("Scripting.FileSystemObject");
   var fh = fso.CreateTextFile("c:\\MyFile.txt", true);
   fh.WriteLine("Some text goes here...");
   fh.Close();
}

</SCRIPT>
</HEAD>

<BODY>
<P>
<SCRIPT language="JavaScript">  WriteFile(); </SCRIPT>
</P>
</BODY>
</HTML>

 

What I do is I put this code in Notepad, save it has hello.html and I then open it in firefox. I then check the file and its still empty, then I open in it Internet Explorer and the same thing happens.

 

Any help appreciated.

Link to post
Share on other sites

If you only need to write to a file, and server side scripting will fit the bill then below I've written a simple piece of php to write to a file.

 

(Tried to keep it as simple as I could....hope that you can understand it.)

 

function writetofile($string,$name,$exists=false){
        //if exists = false then it does not matter if it already exists.
        //if true and the file exists it will return false
        $fixed_extension = ''; //If you want to only allow .txt or lets say .csv to be written then change this and it will be glued as the extension.
        if($exists === true){
            if(file_exists($name . $fixed_extension) === true){
               return false;     
            }
        }
        file_put_contents($name . $fixed_extension,$string);   
        return true;    
    }

if(writetofile('Written at: ' . mktime(),"helloworld.html") === true){
    echo 'Written to file.';   
} else {
    echo 'File exists or there are limitations on the server.';   

}

Link to post
Share on other sites

Thanks for the example :). Sadly PHP doesn't seem to work on my system. I have tried PHP on google chrome, Firefox and IE with no luck. Is there a program I need to download for my system to run PHP ?

 

Im running WIN 7, 32 BIT

 

I have this simple test code

<?php
echo "hello";
?>

 

I save that in notepad and a .php file and try run it with no luck.

 

Thanks...

 

Link to post
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.