Home php How to write data to a txt file?

How to write data to a txt file?




You need to create a script that writes the string “text” to a file located on the client’s computer.


Here is an example of my code

& lt ;?
 $ file = fopen ("D: /file.txt", "a +");
 $ str = "Hello, world! \ n \ r";
 if (! $ file)
  echo ("Error opening file");
  fputs ($ file, $ str);
 fclose ($ file);
? & gt;


Error opening file.

Answer 1, authority 100%

Files cannot be created on the client computer.

Answer 2, authority 60%

PHP scripts are executed on the server side, so create, open, write, etc. files on the client side cannot be done using PHP.

In your example, “D: /file.txt” means that the server will try to open this file on its D drive, not on the client.

Answer 3, authority 20%

I almost misunderstood your question, but here’s an example – creating a file using php:

& lt ;?
 $ fp = fopen ("File_name.txt", "w");
 fwrite ($ fp, "text");
 fclose ($ fp);
? & gt;

Answer 4

Nobody will let your site do anything with the local machine for security reasons. The answer is unambiguous, you can’t do that.

Answer 5

To save data on the client’s machine, you can use the storages intended for this:

  • cookie
    setcookie ('last_visited', time ());

  • localStorage (sessionStorage, etc)
    $ now = time (); echo "& lt; script & gt; localStorage.last_visited = {$ now}; & lt; / script & gt;";

Answer 6

You can also use this option:

$ f = fopen ("file.txt", "a");
fputs ($ f, "text or variable");
fclose ($ f);

Answer 7

  1. Working with files
  2. ftp_get – download a file from an FTP server

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions