How to create and save text file in PHP?

by princess.fritsch , in category: PHP , 2 years ago

How to create and save text file in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by dmitrypro77 , 2 years ago

@princess.fritsch You can use file_put_contents() function in PHP to create and save text file, here is code as example:


1
2
3
4
5
6
<?php

$text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.';
$filename = 'filename.txt';

file_put_contents($filename, $text);


by anahi.murazik , a year ago

@princess.fritsch 

In PHP, you can create and save a text file using the fopen() function to open a file and the fwrite() function to write to it. To save the file, you can use the fclose() function. Here is an example:

1
2
3
$file = fopen("example.txt", "w");
fwrite($file, "Hello, world!");
fclose($file);


In this example, the fopen() function is used to open a file named "example.txt" in write mode ("w"). The fwrite() function is then used to write the string "Hello, world!" to the file. Finally, the fclose() function is used to close the file and save the changes.


You can also use file_put_contents() function to write the contents to the file.

1
file_put