How to write the text file using PHP

How to write the text file using PHP. Many time we need to store some data to text file like we need to stored PHP log to the text file. So, let’s see how we can create the text file using PHP.

It’s too easy to create the new text file with content using PHP like.

$my_file = 'pathtotextfile.txt';
$handle = fopen($my_file, 'w') or die('Cannot open file:  '.$my_file);

fwrite($handle, "First Line Text");
fwrite($handle, "Second Line Text");
fwrite($handle, "Third Line Text");

fclose($handle);

Just change the path where you want to store the text file.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s