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.
7 answers
You cannot create files on the client computer.
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.
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);
?>
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.
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>";
You can also use this option:
$f = fopen("file.txt", "a");
fputs($f, "текст или переменная");
fclose($f);
- Working with files
- ftp_get - uploading a file from an FTP server