How do I write data to a txt file?

You need to create a script that writes the string "text" to a file that is located on the client's computer.

Added.

Here is an example of my code

<?

  $file = fopen ("D:/file.txt","a+");
  $str = "Hello, world! \n\r";
  if ( !$file )
  {
    echo("Ошибка открытия файла");
  }
  else
  {
    fputs ( $file, $str);
  }
  fclose ($file);
?>

Writes

Error opening the file.

 1
php
Author: Nicolas Chabanovsky, 2011-08-28

7 answers

You cannot create files on the client computer.

 5
Author: Андрей Аршинов, 2011-08-28 10:59:04

PHP scripts are executed on the server side, so you can't create, open, write, etc.files on the client side using PHP.

In your example "D:/file.txt" will mean that the server will try to open this file on its D drive, and not on the client.

 3
Author: DemoS, 2011-08-28 13:35:54

I almost didn't understand your question, but here is an example-creating a file using php:

<?
  $fp = fopen ("Имя_файла.txt", "w"); 
  fwrite($fp,"текст");
  fclose($fp); 
?>
 1
Author: Node_pro, 2011-08-28 10:32:20

No one will let your site do anything with the local machine because of security reasons. The answer is clear, you can't do that.

 0
Author: Кирилл Желнов, 2017-04-14 05:17:07

To store data on the client machine, you can use the storage facilities provided for this purpose:

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

  • LocalStorage(sessionStorage, etc)
    $now = time(); echo "<script>localStorage.last_visited = {$now};</script>";

 0
Author: vp_arth, 2017-04-14 05:22:22

You can also use this option:

$f = fopen("file.txt", "a");
fputs($f, "текст или переменная"); 
fclose($f);
 -1
Author: V_Tjuryakin, 2011-08-28 11:22:20
  1. Working with files
  2. ftp_get - uploading a file from an FTP server
 -1
Author: Avalon, 2011-08-28 12:18:49