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
File Handling In PHP
Easy To Learning

Education Needs Complete Solution

Online Ask Question Question And Answer
  1. Home
  2. PHP Tutorials
  3. File Handling 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

File Handling In PHP

PHP File System allows us to create a file, read file line by line, read file character by character, write a file, append file, delete a file and close file.

File Handling

PHP Open File - fopen()

The PHP fopen() function is used to open a file.

Syntax

fopen(filename,mode,include_path,context)

Open File Example

<?php  
$handle = fopen("c:foldermyfile.txt", "r");  
?>  

click for more details

PHP Close File - fclose()

The PHP fclose() function is used to close an open file pointer.

Syntax

fclose(file)

Example

<?php
$file = fopen("mytest.txt","r");
//some code to be executed
fclose($file);
?>

PHP Read File - fread()

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

Syntax

fread(file,length)
Parameter Description
file Required. Specifies the open file to read from
length Required. Specifies the maximum number of bytes to read

Example 1: Read 10 bytes from file:

<?php
$file = fopen("test.txt","r");
fread($file,"10");
fclose($file);
?>

Output : hello php file

Example 2: Read entire file:

<?php
$file = fopen("test.txt","r");
fread($file,filesize("test.txt"));
fclose($file);
?>

 

PHP Write File - fwrite()

The PHP fwrite() function is used to write content of the string into file.

Syntax

fwrite(file,string,length)  

Example

<?php  
$fp = fopen("data.txt", "w");//open file in write mode  
fwrite($fp, "hello");  
fwrite($fp, "php file");  
fclose($fp);  
echo "File written successfully";  
?> 

Output : File written successfully

click for more details

PHP Delete File - unlink()

The PHP unlink() function is used to delete the file.

Syntax

unlink(filename,context)
Parameter Description
filename Required. Specifies the file to delete
context Optional. Specifies the context of the filehandle. Context is a set of options that can modify the behavior of a stream

Example

<?php    
unlink("data.txt"); 
echo "File deleted successfully";  
?>  

click for more details

PHP rename() Function

  • The rename() function renames a file or directory.
  • This function returns TRUE on success or FALSE on failure.

Syntax

rename(oldname,newname,context)
Parameter Description
oldname Required. Specifies the file or directory to be renamed
newname Required. Specifies the new name of the file or directory
context Optional. Specifies the context of the filehandle. Context is a set of options that can modify the behavior of a stream

Example

<?php
rename("images","pictures");
?>
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:
file handling in php pdf file handling in php ppt php file reader php files upload php file example php $_file php file download how to view php files

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.