PHP File Functions

In this tutorial we will learn some of the very important function used for file manipulation. The basic set of operations which can be performed on file is create, open, read, write, close and delete, PHP provides set of functions for all this operations. Let’s learn them all in detail.

Note :-  create one file in the working directory in which you are going to write program for file handling. Give the name of file as MyFile.txt and write following content into it.

Hello DiscussDesk.
This is a Demo File.

fopen()

This function is used to open the file in desired mode.

Syntax

fopen(String FileName, String Mode)

In the syntax Filename can be either the path where the file is stored or just the Filename if the specified file belongs to the same working directory in which you are writing your program. Mode specifies in which mode you want to access the file.

Mode option allows following set of modes.

  • r  used when you want to access file for reading
  • w used when you want to access file for writing puts the file pointer at the beginning of file.  
  • a append mode opens file for writing and puts the file pointer at the end of file.
  • r+ opens the file for reading and writing and puts the file pointer at beginning of file.
  • w+ opens the file for reading and writing and puts the file pointer at beginning of file. It will truncate the contents into the file and creates a file if it does not exists.
  • a+ opens the file for reading and writing and puts the file pointer at end of file. It will append from the end of the file.

fread()

This function is used for reading the contents from the file. It will read the file till the specified length or till the end of file is reached.

To find the EOF(End of File) filesize() function is used. The contents retrieved are of type String.

Syntax

fread(File Handler Variable, int length)

For Ex.

<?php
    $file1=fopen("MyFile.txt","r");
    $string=fread($file1,filesize("MyFile.txt"));
    echo $string;     
?>

Output

Hello DiscussDesk. This is a Demo File.

The above example will open file named MyFile.txt in read mode and it will read the content of file till the end of file is reached.

fwrite()

This function writes the content into the specified file. If the contents are successfully written then integer value 1 is returned otherwise it will return 0.

Syntax

fwrite(File Handler Variable, String Value)

For Ex.

<?php
    $file1=fopen("MyFile.txt","w");
    $string="This contents will be written to File";
    fwrite($file1,$string);
?>

Output

Check the file contents it will write the string in file.

fclose()

This function closes the file pointer which is opened by fopen().

Syntax

fclose(File Handler Variable)

file_exists()

This function checks whether the file already exists in your system or not. If the file exists it returns true otherwise it returns false.

Syntax

file_exists(String FileName)

For Ex.

<?php
    if(file_exists("MyFile.txt"))
    {
       echo "MyFile.txt Already Exists";
    }
    else
    {
       $file1=fopen("MyFile.txt","w");
    }
?>

In our case we have already created one file MyFile.txt so the program will give output “MyFile.txt Already Exists” , but in case if the file does not exists then a new file MyFile.txt will be created and opened in write mode.

copy()

This function will copy the content of source file to the destination file. If the destination file does not exists it first creates the file and then copies the content of the source file into it.

Syntax

copy(String Source File, String Dest File)

For Ex.

<?php
    $file1="MyFile.txt";
    $file2="YourFile.txt";
    copy($file1,$file2);
?>

rename()

This function will rename the name of file from old name to new name specified.

Syntax

rename(String OldName , String NewName)

For Ex.

<?php
    $old="MyFile.txt";
    $new="Pratik.txt";
    rename($old,$new);
?>

filesize()

It will get the size of the file, which means the total number of characters is returned. The return type is integer.

Syntax

filesize(String FileName)

For Ex.

<?php
    $file1="Pratik.txt";
    echo "File Size ".filesize($file1);
?>

Output

File Size 37

fileatime() , filemtime()

fileatime() will return the time when the file was last accessed. filemtime() will return the time when the file was modified last. The return type is time in integer.

Syntax

fileatime(String FileName)

filemtime(String FileName)

For Ex.

<?php
    $file1="Pratik.txt";
    echo "File Last Accessed on ".fileatime($file1)."<br>";
    echo "File Last Modified on ".filemtime($file1);
?>

move_uploaded_file()

This function will move the uploaded file into new location. It returns true if the file is moved successfully else it will return false. It’s the most important function used in file manipulation.

Syntax

move_uploaded_file(String Source, String Dest)

For Ex.

<?php
    $source="Pratik.txt";
    move_uploaded_file($source,"C:\");
?>

The above code will move the file named "Pratik.txt" into C:\ folder.

So to conclude in this tutorial we have learnt some of the important functions used for file manipulation.

Share this Article on Social Media

Get Updates, Scripts & Other Tutorials to Directly to your Email

Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.

Comments