Easy To Learning
  • Login
  • Sign Up
  • Q&A
  • Home
  • Php Tutorial
  • MySql Tutorial
  • Object-Oriented Programming
  • Java Tutorial
  • HTML Tutorial
  • React JS Tutorials
  • Free Projects
  • Blogs & Articles
  • Others
    • About Us
    • Contact Us
Read Files In PHP
Easy To Learning

Education Needs Complete Solution

Online Ask Question Question And Answer
  1. Home
  2. PHP Tutorials
  3. Read Files In PHP

PHP Tutorial

Basic PHP Fundamentals

  • PHP Tutorial Content
  • PHP Introduction
  • PHP History
  • Uses of PHP
  • PHP Features
  • What Is PHP
  • PHP Installation And Software Requirements
  • How To Install Wamp Server
  • How To Install Xampp Server
  • Php Syntax
  • PHP Variables
  • PHP Data Types
  • PHP Constants
  • PHP Echo And Print
  • PHP Operators
  • PHP Assignment Operators
  • PHP String And Logical Operators
  • Operators Categories
  • PHP Comments
  • PHP $ And $$ Variables
  • Super Global Variables

PHP Control Statement

  • PHP Control Structure
  • PHP If Statement
  • If Elseif Statement In PHP
  • Switch Statement In PHP
  • Break Statement In PHP
  • If Elseif Else Statement
  • Continue Statement In PHP

Php Loops

  • What Is Loop
  • For Loop In PHP
  • PHP Foreach Loop
  • PHP While Loop
  • Do While Loop

PHP Arrays

  • PHP Arrays
  • Indexed Arrays
  • Associative Array
  • Multidimensional Arrays
  • Array Functions
  • Array Constants
  • List of Array Functions
  • Array Functions First Exercise

Php String

  • String In PHP
  • PHP String Functions

PHP Functions

  • PHP Functions
  • PHP Parameterized Function
  • PHP Call By Value
  • Call By Reference In PHP
  • Default Argument Values Function In PHP
  • Variable Argument Function In PHP
  • Recursive Function In PHP
  • Math Function In PHP
  • Math Function Examples

PHP Form

  • Form Handling In PHP

PHP Include

  • Include File In PHP

PHP State Management

  • Cookies In PHP
  • Session In PHP

PHP File System

  • File Handling In PHP
  • Open Files In PHP
  • Read Files In PHP
  • Write Files In PHP
  • Delete Files In PHP
  • Append To Files In PHP

PHP Upload & Download

  • File Upload In PHP
  • Download File In PHP

PHP Mail System

  • Mail System In PHP
  • Mail With Attachment In PHP

Basic PHP Program

  • Php Programs
  • Sum Of Digits
  • PHP Even Odd Program
  • Prime Number Program
  • Table Of Number Program
  • Factorial Of A Number
  • Armstrong Number
  • Palindrome Number
  • Fibonacci Series

Read Files In PHP

PHP provides various functions to read data from a file. There are different functions that allow you to read all file data, read data line by line and read data character by character.

This function returns the number of bytes read on success, or FALSE and an error on failure. You can hide the error output by adding an "@" in front of the function name.

PHP file read functions are given below.

  • fread()
  • fgets()
  • fgetc()

PHP Read File - fread()

The PHP fread() function is used to read data of the file. It requires two arguments: file resource and file size.

Syntax

string fread (resource $file, int $length )  
  • $file represents file pointer that is created by fopen() function.
  • $length represents the length of byte to be read.

PHP Read File Example

<?php    
$filename = "Your Directory Path\fileName.txt";    
$fp = fopen($filename, "r");//open file in read mode    
$fileContents = fread($fp, filesize($filename));//read file    
echo "<pre>$fileContents </pre>";//printing data of file  
fclose($fp);//close file    
?>    

Output :

This is Php Tutorials

Online Learning Website

I Am Software Developer

PHP Read File - fgets()

The PHP fgets() function is used to read a single line from the file.

Syntax

string fgets ( resource $handle [, int $length ] )  

PHP Read File - fgets() Example

<?php    
$fp = fopen("Your Folder Path\myFile.txt", "r");//open file in read mode    
echo fgets($fp);  
fclose($fp);  
?>  

Output

this is first line

PHP Read File - fgetc()

The PHP fgetc() function is used to read single character from the file. To get all data using fgetc() function, use !feof() function inside the while loop.

Syntax

string fgetc ( resource $handle )  

PHP Read File - fgetc() Example

<?php    
$fp = fopen("Your Directory Path\fileName.txt", "r");//open file in read mode    
while(!feof($fp)) {  
  echo fgetc($fp);  
}  
fclose($fp);  
?>    

Output

this is first line this is another line this is third line

Series: PHP Tutorials Hub
All Topics
Free Download Projects
Previous Chapter
Next Chapter

Ask a Question

Have a coding query or doubt? Ask here and get help from our community.

500 characters remaining
Submitting...
Free Download Projects
Join Telegram Group Join WhatsApp Group
Related Topics:
php read file into string php read file into array php read file line by line php fread php fgets php read json file php $_file readfile example

Member Sign In

Forgot?
Signing in...

Don't have an account? Register here

Create Free Account

Register Captcha
Registering...

Already have an account? Sign In

Ask a Question

500 characters remaining
Submitting...

About Easy To Learning

Easy To Learning is a comprehensive programming learning portal offering free, practical, developer-driven tutorials for PHP, Java, Python, Spring, MySQL, CSS, Bootstrap, CakePHP, and more.

Explore community-contributed questions and answers, and download ready-to-run source code projects.

Quick Links

  • Home
  • Questions & Answers
  • Download Projects
  • Blog & Articles
  • Member Login

Community

  • Telegram Group
  • WhatsApp Community
  • Ask a Question
  • Join as Member

Connect With Us

Stay updated with our newest programming tutorials and free open source projects.

© 2026 Easy To Learning. All Rights Reserved.