Web Info and IT Lessons Blog...

Wednesday, 1 April 2015

PHP File Handling

The previous lesson of PHP Lessons Series was about how to upload an image in PHP and in this lesson we will learn about file handling in PHP.

File Handling in PHP

File handling is used in php to process files data. Through file handling we can read data from a file or write data to a file or do both read and write. The action (i.e read and write) to be performed on the file is decided from the mode in which the file is opened. The modes in which a file can be opened are as follows:

r - opens a file in read mode.
w - opens a file in write mode. In this mode all the previously written data in the file is erased and new data is added to the file.
a - opens a file in write mode. In this mode the previously written data in the file is not erased and the new data is appended to the existing data. So this is basically append mode.
x - creates a file for write mode only.
r+ - opens a file for both read and write mode.
w+ - opens a file for both read and write mode.
a+ - opens a file for both read and write mode. Newly written data is appended to the existing data.
x+ - creates a file for both read and write mode.

Now let us start with reading a simple file in php.

How to read a file in php?

Let us say we want to read a simple text file in php, the simplest method to achieve this is through readfile() function i.e


$output = readfile("testfile.txt");
echo $output;

The output of above code will be the text written in the file and the number of bytes readfile() function had to read. The number of bytes is simply the number of characters written in the file.

The second method is more efficient and reliable way of reading a file. In this method we have to open the file first and then read it's contents. After successfully reading the contents we close the file. File can be opened in any of the above mentioned modes. PHP code of opening a file in read mode and reading it's contents is given below:


$file = fopen("testfile.txt", "r") or die("Could not open the file!!!");
$output = fread($file,filesize("testfile.txt"));
fclose($file);
echo $output;

The output of the above code will only be the text written in the file and not the number of bytes the function had to read. In the above fread() function the first parameter passed is the source of the file and the second parameter is the number of bytes to read from the source file. As we have passed the filesize of the source file as the second parameter, so it will read the complete file.

Practical Implementation:
Let us look at the practical implementation of file reading using an html form. We will use an html form element to read the name of a file and then read the contents of the file name passed in the html form.

Html Code:

<form method="POST" action="#">
Enter the name of text file:<br />
<input type="text" name="file_name" /><br /><br />
<input type="submit" name="submit"/>
</form>

PHP Code:

if(isset($_POST['submit'])){
 $file_name = $_POST['file_name'];
 $openfile = fopen($file_name.".txt", "r") 
        or die("Could not open the file!!!");
 $output = fread($openfile,filesize($file_name.".txt"));
 fclose($openfile);
 echo $output;
}

How to write to a file in php?

Just like file reading, file writing also needs an opened file to write into. The difference is that in file reading if you try to open a file that does not exists, you will get an error but in file writing if you try to open a non existing file, you won't get an error but a file with the specified name will be created automatically and then opened to write into. An example code of opening a text file in write mode and writing to it is given below:


$file = fopen("testfile.txt", "w") or die("Could not open the file!!!");
$sampleText = 'This is test sample text!';
fwrite($file, $sampleText);
fclose($file);

Practical Implementation:
In the practical implementation we will use an html form to take file name and the text to write to the file as inputs and write the text to the file.

Html Code:

<form method="POST" action="#">
Enter the name of text file:<br />
<input type="text" name="file_name" /><br /><br />
Enter the text to write:<br />
<textarea rows="8" cols="30" name="text" ></textarea><br /><br />
<input type="submit" name="submit"/>
</form>

PHP Code:

if(isset($_POST['submit'])){
 $file_name = $_POST['file_name'];
 $text = $_POST['text'];
 $openfile = fopen($file_name.".txt", "w");
 fwrite($openfile, $text);
 fclose($openfile);
}

For more lessons about PHP subscribe on InformationBitz.com

No comments:

Post a Comment