How do I open a file to write content to?

Answer Posted / jude jeevanraj.p

Working with files and folders in PHP is fairly easy though
it can take a little while to get used to.

You need to create a filehandle, which is a pointer to a
file, that you wish to write too.

Use the fopen function to open a file, and pass the name of
the file as the first parameter and what you want to do
with it second - in this case 'w' for write.

Then simple use fwrite to write to your file, and close it
with fclose.

Here's a simple example that will create a file in the
directory called example.txt and write to it with just a
few lines of PHP code.

<?php

$myfile = fopen("example.txt","w");
fwrite($myfile,"That was easy!");
fclose($myfile);
?>

Is This Answer Correct ?    5 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is psr in php?

693


What is the best website to learn php?

600


How cookies are trported from browsers to servers?

649


What should we do to be able to export data into an excel file?

604


Why and where do we use htaccess?

619






What is php sequence?

616


Where are php configuration settings stored?

616


How is the ternary conditional operator used in php?

586


Tell me what are magic methods?

628


How to call a php function from html button?

683


What is the meaning of a persistent cookie?

631


Is php good for career?

619


Write a php function to convert all null values to blank?

902


How long does a php session last for?

642


How can you encrypt password using php?

582