PHP – File Write

by Hiroshi on June 23, 2008

in File, File Manipulation

Now that you know how to open and close a file, lets get on to the most useful part of file manipulation, writing! There is really only one main function that is used to write and it’s logically called fwrite.

PHP – File Write: fwrite Function

We can use php to write to a text file. The fwrite function allows data to be written to any type of file. Fwrite’s first parameter is the file handle and its second parameter is the string of data that is to be written. Just give the function those two bits of information and you’re good to go!

Below we are writing a couple of names into test file testFile.txt.

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Some text\n";
fwrite($fh, $stringData);
$stringData = "Hello\n";
fwrite($fh, $stringData);
// Output:
// Some text

Related Posts

Previous post:

Next post: