Writing to Files

This section describes how to use PHP to write content to files on Windows systems.

PHP includes the fwrite() function for writing files. The function is defined below:

fwrite(resource handle,string) - writes the contents of string to the file stream pointed to by handle. If the length argument is given, writing will stop after length bytes have been written or the end of string is reached, whichever comes first.

The following example illustrates how to write the entire contents of a file:

filewrite.php

<?php

$filename = "C:/Documents and Settings/Administrator/MyFiles/myfile.txt";

$newfile = fopen($filename, "w") or exit("Could not open file);

$file_contents = "Add this string to the text file";

fwrite($newfile,$file_contents);

fclose($newfile);

?>

The first step is to create a variable to hold the full path to the file that will be opened or created:

$filename = "C:/Documents and Settings/Administrators/MyFiles/myfile.txt";

The path to the text file, myfile.txt, is stored in the variable called filename. Next, a file pointer, called $newfile, is created and used with the fopen() function to open the file specified in the previous section. A file pointer is used to refer to the just-opened file. The file is opened in write mode:

$newfile = fopen($filename, "w");

The file pointer is a PHP variable that contains a reference to the opened file. It will be used later to write content to the opened file.

Next, a variable called $file_contents is created and is assigned a string value that will be written to the text file, myfile.txt.

Finally, the fwrite() function is called. The first parameter of the fwrite() function refers to the name of the file to which content will be written. The second parameter contains the text that will be written to the opened file.

fwrite($newfile, $file_contents);

After all file processing is complete, the fclose() function is used to close the open file.

In some cases it may be necessary to write the contents of an existing file to a new file. This process requires the use of the fopen(), fread(), and fwrite() functions. The first file is opened, it's content is read, and is written to a new file which has also been opened. The following script illustrates this type of process:

filecopy.php

<?php

$fileAname = "C:/MyFiles/PHP/file1.txt";
$fileBname = "C:/MyFiles/PHP/file.txt";

$currentfile = fopen($fileAname,"r") or exit("Could not open file");
$fileAcontents = fread($currentfile,filesize($fileAname));

$newfile = fopen($fileBname,"w");

fwrite($newfile, $fileAcontents);

fclose($newfile);
fclose($currentfile);

echo "Contents copied file1.txt to file.txt";

?>

This script copies the contents of "file1.txt" to a new file, "file.txt". First, two variables, $fileAname and $fileBname are declared and assigned the directory paths for the existing and the new file. The fopen() function is used to open the current file to read its content. The opened file is assigned to the file pointer $currentfile. The contents of the opened file are read using the fread() function and assigned to the variable $fileAcontents. Next, the fopen() function is used again to open the new file. The opened file is assigned to the file pointer $newfile. The fwrite() function is used to write the contents of the original file to the new file. Once the copy process is complete, both files are closed with the fclose() function.