Web Development Tutorials




  
  

Writing to Files

In addition to reading file content, PHP also allows content to be written to a file using the fwrite() function. Content can be written to an existing file or if the specified file does not exist, it will be created. The following example illustrates how to write the entire contents of a file:

filewrite.php

<?php

$filename = "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 = "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); 

When writing to a file, the "w" and "a" file open modes can be used. If the "w" mode is used, any existing content in the file will be replaced with the new content since this mode places the file pointer at the beginning of the file prior to the write process. If the “a” mode is used, the new content is appended to the existing content since the file pointer is placed at the end of the file prior to the write process.

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.


TOP | NEXT: Copying Files