php

FILE System


 

PHP provides a variety of functions and features for interacting with the file system. Here's a brief overview of the file system operations can be performed in PHP:

 

PHP FOPEN():

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

 

Syntax:

<?php
    resource fopen ( string $filename , string $mode [,
bool $use_include_path = false [, resource $context ]] )  
?>

 

$filename: Specifies the name of the file or URL to open. It can be a local file or a URL.

$mode: Specifies the mode in which to open the file. This is a string that can contain one or more of the following characters:

 

  • 'r': Open for reading only.
  • 'w': Open for writing only.
  • 'a': Open for writing only.
  • 'x': Create and open for writing only. If the file already exists, the call to fopen() will fail, returning false and generating an error of level E_WARNING. If the file does not exist, attempt to create it.
  • 'r+': Open for reading and writing.
  • 'w+': Open for reading and writing.  Truncate the file to zero length. If the file does not exist, attempt to create it.
  • 'a+': Open for reading and writing. place the file pointer at the end of the file. If the file does not exist, attempt to create it.
  • 'x+': Create and open for reading and writing. If the file already exists, the call to fopen() will fail, returning false and generating an error of level E_WARNING. If the file does not exist, attempt to create it.
     

Example

 

<?php
    // Specifing the file name
    $filename = "file.txt";

    // Open the file for reading
    $file = fopen($filename, "r");
?>

Explanation:

The $filename is the name of the file we want to open.
We use fopen($filename, "r") to open the file in read mode.

 

PHP FCLOSE():

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

 

Syntax:

<?php
    bool fclose(resource $handle);
?>

 

Example:

<?php
    // Close the file
    $closed = fclose($file);
?>

 

PHP FREAD():

The fread() function is used to read the content of a file in PHP.

 

Syntax: 

<?php
    string fread(resource $handle , int $length); 
?>

Note: First argument is resource & second argument is file size.

 

Example: 

<?php
    $filename = "file.txt";
    //open file in read mode
    $handle = fopen($filename, "r");    
    
    //read file
    $contents = fread($handle, filesize($filename));
        
    //printing data of file
    echo $contents; 
    //close file 
    fclose($handle);    
?>

 

PHP FWRITE():

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

 

Syntax: 

<?php
    fwrite(resource $handle , string $string [, int $length ] );
?>

 

Example:

<?php  
    //open file in write mode
    $fo = fopen('test.txt', 'w');
    // write in a open file
    fwrite($fo, 'hello ');  
    fwrite($fo, 'Geeks');  
    fclose($fo);  
?> 

 

PHP UNLINK():

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

 

Syntax: 

<?php
    bool unlink(string $filename [, resource $context ] )  
?>

 

Example:

<?php
    unlink('test.php');
?>

 

PHP COPY():

The copy() function is used to copy a file in PHP.

 

Syntax:

<?php
    copy($sourceFilePath, $destinationFilePath);
?>

 

Example:

<?php
        $sourceFile = 'path/sourceFile.php';
        $destinationFile = 'path/destinationFile.php';

        copy($sourceFile, $destinationFile);
    ?>

 

PHP RENAME():

The rename() function is used to rename a file in PHP.

 

Syntax:

<?
    rename($oldFilePath, $newFilePath);
?>

 

Example:

<?php
    $oldFilePath = '/oldPath/file.php';
    $newFilePath = '/newPath/file.php';
    
    rename($oldFilePath, $newFilePath);
?>

 

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