We have already learned the basics of file handling, such as opening, creating, closing and reading a file. Today we will learn how to write to a file in this PHP File Create/Write tutorial.
- Writing to a file:
- We can write into a file using fwrite() function.
- It takes 3 parameters,
- The file pointer that points to the file in which we want to write.
- The string to be written in the file.
- The number of bytes that must be written at a time in the file. It is optional. If the number of bytes is not provided, the whole given string will be written into the file.
- If we want the statements provided in the string be written each on a new line, a new line character (\n) should be used there.
- Remember, windows require a carriage return character as a new line character, so to provide new line in windows the new line character to be used is \r\n.
- So let us go through an example that writes some data to a file:
- To demonstrate it, create a new folder named as file_write folder in the htdocs folder in xampp folder located in C drive. Then open a new notepad++ document and save it as index.php in this newly created file_write folder.
- The code is given below:
<html> <head> <title>Write into File using PHP</title> <style> .decor{ padding:20px; color:red; font-size:25px; font-width:bold; } </style> </head> <body> <h1>Use of fwrite() function to write to a file</h1> <?php if(!($fp=fopen("fileinfo.txt","w"))) { echo '<p class="decor">Problem in creating the file!</p>'; } else { //string to be written $string="File handling in PHP is mostly similar to file handling in standard C language.\r\n"; //use of fwrite() function to write to file fwrite($fp,$string); //string to be written $string="PHP provides various tools for creating, uploading and editing files.\r\n"; //use of fwrite() function to write to file fwrite($fp,$string); echo '<p class="decor">Write Completed!</p>'; } fclose($fp); ?> </body> </html>
fwrite($fp,$string);
- Appending data to a file means, adding information to an existing file by retaining its original data.
- To append data to an existing file, the file should be opened in append mode “a”.
- Let us append data to our existing info.txt file and read it in the same program.
- This can be done by opening the file info.txt in “a+” mode i.e. read/append mode.
- The code is given below:
<html> <head> <title>Write into File using PHP</title> <style> .decor{ padding:20px; color:red; font-size:25px; font-width:bold; } </style> </head> <body> <h1>Use of fwrite() function to write to a file</h1> <?php //opening file in a+ mode and writing to it if(!($fp=fopen("fileinfo.txt","a+"))) { echo '<p class="decor">Problem in creating the file!</p>'; } else { $string="You need to be very careful while dealing with files, because you can harm the file data if something goes wrong.\r\n"; $string.="Any information written in the file is temporarily written in the buffer and is then later stored on the hard-disk when the file is saved.\r\n"; fwrite($fp,$string); //write string to file info.txt rewind($fp); //move the file pointer to the start of the file while(!feof($fp)) { $s=fgets($fp); echo $s."<br>"; } } fclose($fp); ?> </body> </html>
rewind($fp);
while(!feof($fp)) { $s=fgets($fp); echo $s."<br>"; }
Function | Syntax | Description |
---|---|---|
fread() | fread(file_pointer,number of bytes to read); | The function fread() reads number of bytes specified from the file pointed by file pointer. |
readfile() | readfile(“filename”); | The function readfile() reads the file passed to it as a parameter and outputs it to output buffer. |
copy() | copy(file,file_to); | It copies the first file contents to the second file. Returns true on success and false on failure. |
dirname() | dirname(“path”); | It returns the directory name component from the given path. |
ftell() | ftell(filename); | It returns the current file pointer position in the given file otherwise returns false on failure. |
is_dir() | is_dir(filename); | Checks whether the file is a directory. |
is_file() | is_file(filename); | Checks whether the file is a regular file. |
table 1
Thus we learned how to write into a file and then read from the file along with introduction to some more functions dealing with file handling in this PHP File Create/Write tutorial.