ImranP Posted February 27, 2011 Share Posted February 27, 2011 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. Quote Link to comment Share on other sites More sharing options...
trq Posted February 27, 2011 Share Posted February 27, 2011 You cannot write to file on a client PC using Javascript. Quote Link to comment Share on other sites More sharing options...
ImranP Posted February 27, 2011 Author Share Posted February 27, 2011 How would I go about sending a string to the server computer to be put into a .txt file ? Quote Link to comment Share on other sites More sharing options...
haku Posted March 1, 2011 Share Posted March 1, 2011 You need to create a script on the server that receives the string, and have that script write the file. Quote Link to comment Share on other sites More sharing options...
ImranP Posted March 1, 2011 Author Share Posted March 1, 2011 You need to create a script on the server that receives the string, and have that script write the file. Thanks for the reply. Would you have any example code for me ? Quote Link to comment Share on other sites More sharing options...
optioned Posted March 7, 2011 Share Posted March 7, 2011 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.'; } Quote Link to comment Share on other sites More sharing options...
ImranP Posted March 7, 2011 Author Share Posted March 7, 2011 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... Quote Link to comment Share on other sites More sharing options...
trq Posted March 7, 2011 Share Posted March 7, 2011 Is there a program I need to download for my system to run PHP ? Indeed, you need a web server configured to execute PHP. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.