PHP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
PHP Topics
Introduction Introduction
Syntax Syntax
Data Types Data Types
Operators Operators
Control Structures Control Structures
Functions Functions
Pre-defined Function Pre-defined Function
Calendar Functions Calendar Functions
Date and Time Date and Time
Array Functions Array Functions
Array List Array Functions List1
Array Function List Array Functions List2
Math Functions Math Functions
PHP MYSQL Functions PHP Mysql Functions
File Handling File Handling
Error Handling Error Handling
DB Size DB Size
PHP Mail PHP Mail
String Tokens String Tokens
String Functions String Functions
String Functions List String Functions List1
String Functions List2 String Functions List2
Session Functions Session Functions
Cookies Functions Cookies Functions
Form Variables Form Variables
Running PHP from JS Running PHP from JS
Array To JS Array To JS
JS Array Array to PHP
Encryption Encryption
Common Header Common Header
Forums Ask Your Doubts
Scraps More about PHP
Feedback Feedback
 




Reading From file in php


Tutorials »Php »

Topic

How to Read a file using php?



Explanation

Reading and printing the contents in a file.
For an example we will use the file test.txt to explain it

Step 1:
Define the file name in to a variable

Example: $file = "./test.txt";

Step 2:
We will open a stream (connection) with the file using the method or php function fopen('filename','type')

Example: $open = fopen($file, "r");
The method fopen takes two arguments, first the file name and second argument that determines the file-opening mode. To read a file it should be 'r'.

Step 3:
Now we will find the size of the file using the php method filesize('filename');

Example: $fsize = filesize($file);
This method takes the file name as argument and returns the size of the file in Bytes.

Step 4:
We will read the content in the file using the php function fread('stream','size')

Example: $fsize = fread($open,$fsize);
We have passed two arguments to the method fread(). The first argument is the stream obtained in step2. The second argument is the size that has to be read from the file. To read the full file we have pass the full size of the file.


Example Code:
<?php
$file = "./test.txt";
$open = fopen($file, "r");
$size = filesize($file);
$count = fread($open, $size);
echo($count);
?>

The Result is
Name - HIOX INDIA Topic - tutorials





A Note

Learn PHP programming language tutorial with simple and neat example. Hope you enjoy this free tutorial. Do give us your valuable feedback and suggestions on this online tutorial. This is a Copyright Content.

Other Links

web hosting