How to read write text-files in PHP

 How to read / write /copy file content in PHP

 


 

PHP provided several predefined functions to read or write data from files. It is very common for developers to copy data from one source to another. File functions in PHP make things easier. In this article, we will use different types of file function to read data from files.


Table of Contents


To read a text from a file, we use the following methods in PHP

  • fopen()
  • feof()
  • fread()
  • fgets()
  • fclose()

fopen :   function returns a file handle(file pointer)
fread :    reads specified no. of bytes
fgets :     reads line of text every time.
feof :      checks for end of file. Returns boolean value.
fclose :   closes an opened file. File must be closed when it is opened.

 



Reading text Files


 
fopen Example:
 
 
  
index.php
 
<?php
 
$file_handle = fopen("about.txt", "r");
 
print $file_handle ;
 
fclose($file_handle);
 
?>

 
o/p:
Resource id #2
 
 

Resource id #2 is a pointer to the file you want to open. 

 



 

To read data from a file, you can use fread().

First Open notepad and create a text file called about.txt with the content given below.

Example:

about.txt

If you are looking for an extremely wide range of courses that you just simply can not find anywhere else, then look no further. Here at e-Trainings  we boast over various programming and web designing and web development courses that we are providing  at  online We know that you have a busy schedule, and we want to work around that.

 

 

Example: 
 
<?php
 
$file = fopen("about.txt", "r");
 
//reads max. of 100 bytes from a given file.
  
$content = fread($file, 100);
 
print $content;

fclose($file);
 
?>
 
output
If you are looking for an extremely wide range of courses that you 
just simply can not find any wher 
 
 


 
 
 

Reading complete file using filesize() method. 

 
Example
 
<?php
 
$filename = "about.txt";

$file = fopen($filename, "r") or die("Couldn't open $filename");
 
$content = fread($file, filesize($filename)); 
 
print $content;

fclose($file);
 
?>
 
 

output:

If you are looking for an extremely wide range of courses that you just simply can not find anywhere If you are looking for an extremely wide range of courses that you just simply can not find anywhere else then look no further. Here at e-Trainings we boast over various programming and web designing and web development courses that we are providing at online We know that you have a busy schedule, and we want to work around that. 




fgets: To Read text line by line



open a note create file with name as demo.txt
with following content
 
demo.txt  
 
PHP can generate dynamic page content
PHP can create, open, read, write, delete, and close files on the server
PHP can collect form data
PHP can send and receive cookies
PHP can add, delete, modify data in your database
PHP can be used to control user-access
PHP can encrypt data





Example:
 
<?php

$file = fopen("demo.txt", "r");

while (!feof($file)) {

$line = fgets($file); //fgets reads a line of text at a time from a given file.

print $line . "<br>";

}

fclose($file);

?> 


fgets() and fread() - What is the difference?


The function fgets reads a single line from a text file. It is reading so long until the end of the current line (or the end of the file) is reached. Therefore, if you would like to read one line from a text file, you should use fgets. The function fread not only reads until the end of the line but to the end of the file [e.g. fread ($handle)] or as many bytes as specified as a parameter [e.g. fread($handle, 1024)]. So, if you would like to read a complete file, no matter whether it is a text file with all containing lines or arbitrary raw data from a file, you should use fread.



  • If you want to read a line, from a text file, then use fgets
  • If you want to read some data (not necessarily a line) from a 
    file, then use fread.
 
 

Writing text Files

 
 


 

 fwrite(): available with version 5 PHP. To open for writing, we use fopen()

And mode is --"w".
 
 
Example:
 
 
 
<?php

 
$file = fopen("sample.txt", "w");

$data = "Some sample text data";  
 
fwrite($file, $data);

fclose($file);

print "file created and written to sample.txt". "<br>";

?> 

output
sample.txt is created in current folder 




Append


To add Data to an existing file, use file mode -- 'a'.

Example:   
 
<?php

$file = fopen("sample.txt", "a");

$data = "Some more sample text data"
 
fwrite($file, $data);

fclose($file);

print "file created and written to". "<br>";

?> 



File Modes we used in this blog are

r -- Use this to read a file only. The pointer is set to the start of 
     the file.
w -- Creates a new file with a given filename. If already exists that 
     name it overwrites.
a -- Append data to the end of the file. This is also used to write to 
     a file.
 
There are some more available in PHP.  
 
r+ → Use this to read and write to a file. The pointer is set to 
   the start of the file.
w+
Same as "w", but used to read and write.
a+
Same as "a", but with read access as well.
x
Create a file to write only.
x+
Same as x but with read access as well.
rb
Force PHP to open the file in binary mode.





Download the code examples Here



Copy text Files


Copy() function

 
To copy one file to another, there is a copy() function in PHP . It returns a boolean value( digit one(1)) if file copy succeeds.
 
Note: If the destination file already exists, it will be overwritten.

 
 
Example:   
 
<?php

$result = copy("sample.txt", "sample2.txt"); 


if($result)
{
 print "file copied";
}
 
else{ print "Not copied";}
 
?> 
 
Result: new sample2.txt text is reated
 
 
 

Comments

Popular posts from this blog

Using javascript pass form variables to iframe src

Creating a new PDF by Merging PDF documents using TCPDF

Import excel file into mysql in PHP