Php

PHP file Handling Methods

This PHP tutorial help to understand PHP 7 file handling methods with examples.PHP has in-built file handling methods for common file operations.

What is a File?

A file is simply a resource for storing information on a computer.

Usually, files are used to hold information like:

  • Program configuration options
  • simple information like contact names and phone numbers.
  • photographs, images, etc.

PHP provides a set of in-built functions to handle files. Some of the functions are, fopen(), file_exists(), file_get_contents() and etc.

The following list includes some of the fundamental file-related operations.

  • Opening a file
  • To read, write and append data into a file
  • Closing a file

Here, We’ll disccuss here some PHP file handlimng methods like:

  • file_exists() method
  • fopen() Function
  • fwrite() Function
  • fclose() Function
  • fgets() Function
  • copy() Function
  • file_get_contents() Function
  • deleting a File

Mode of Operation

In PHP file handling, there are four sets of possible modes. These are,

  • {r and r+} – To read the existing files.
  • {w and w+} – To change the entire file content.
  • {a and a+} – To add content after existing file content.
  • {x and x+} – To create a new file and work with it.

PHP file_exists() Method

This file_exists() method helps to check whether the file is there in the directory before processing it. If the file you are looking for is not on the server, this PHP function also helps you in creating a new one.

Syntax:

file_exists($file_name);

Whereas Parameters:

$file_name: variable is the file path.

It returns TRUE if the file exists in the directory or FALSE if the file doesn’t exist/found in the server/server directory.

Example Code

The sample PHP code to check the file exists.

<?php
If(file_exists('file_name.txt'))
{
echo "The File Found!";
}
else{
echo "your file_name.txt doesnot exist!";
}
?>

PHP fopen() Method

This fopen() method helps to open a file with the corresponding mode.

Syntax:

fopen($file_name, $mode, $use_include_path,$context);

Whereas Parameters:

  • $file_name: variable is the file path.
  • $mode: reading, writing, appending, etc.
  • $use_include_path: optional
  • $context: optional

Example Code

The sample PHP code to open a file.

<?php
$op = fopen("file_name.txt",'w');
or
die("Failed in creating the file");
?>

We have passed ‘w’ mode, It’ll perform the write operation only.

The entire file content will be cleared and the pointer will focus the start position of the file content. This method is used to change the existing file content, completely.

PHP write() Method

This write() method helps to write contents into a file.

Syntax:

fwrite($handle,$string,$length);

Whereas Parameters:

  • $handle: This is a file pointer’s.
  • $string: This is the data/information which is to be written inside the file..
  • $length: optional, This helps to specify the maximum file length.

Example Code

The sample php code to write a file.

Related Post
<?php
$op = fopen("hello.txt", "w");
fwrite($op, "PHPFlow: php tutorials\n");
?>

First, clear the content of hello.txt and focus on the start position of the file content. This method is used to change the existing file content completely.

PHP fclose() Method

This fclose() method help to close the file which is opened already in the server.

Syntax:

fclose($handle);

Whereas Parameters:

  • $handle: This is a file pointer’s.

Example Code

The sample php code to close a file.

<?php
$op = fopen("hello.txt", "w");
fwrite($op, "PHPFlow: php tutorials\n");
fclose($op);
?>

The above code writes the contents into the file then closed the file.

PHP fgets() Method

This fgets() method help to help to read the file/files line by line.

Syntax:

fgets($handle);

Whereas Parameters:

  • $handle: The resource of file pointer’s.

Example Code

The sample PHP code to read file line by line.

<?php
$op = fopen("hello.txt",'r');
$lines = fgets($op);
echo $lines;
fclose($op);
?>

The above code helps to print all the contents of the hello.txt file then close the file.

PHP copy() Method

This copy() method helps to copy the files.

Syntax:

copy($file, $file_copied);

  • $file: The source file path which is to be copied.
  • $file_copied: The name of the copied file.

Example Code

The sample PHP code to copy a file.

The above code will copy the file hello.txt content into hello1.txt.

PHP file_get_contents() Method

This file_get_contents() method helps to n reading the entire contents of the file.

The main difference between the fgets() and file_get_contents() is that file_get_contents will return the whole data as a string but the fgets will be red the whole file line by line.

Syntax:

file_get_contents($file, $file_copied);

  • $file: The source file path which is to be copied.
  • $file_copied: The name of the copied file.

Example Code

The sample PHP code to copy a file.

Deleting a File (unlink() Function)

The unlink() method helps to delete the file from the target path.

Syntax:

unlink($file);

Whereas Parameters:

  • $file: The source file path.

Example Code

The sample PHP code to delete a file.

<?php
if(!unlink('hello1.txt'))
{
echo "Error! delete the file";
}
else
{
echo "The file has been deleted successfully!";
}
?>

Recent Posts

Configure and Retrieve S3 Information Using Laravel PHP-AWS-SDK

This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More

3 months ago

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

9 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

9 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

9 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

10 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

10 months ago

Categories