How to Read/Create a file in PHP ?

In this example we will see how to Open/Create a file in PHP and read it .

To open/create a file in PHP fopen() is use .
Syntax: fopen(filename,mode);
This function takes 2 parameter to open/create a file  . If file exist then it will simply open it if not then create a file then open it . This function return a file.

To read file in PHP fread() is use.
Syntax: fread(file,filesize);
This function takes 2 parameter to read file . First parameter is file (which file you want to read) . Second parameter is file size (max file bytes to read).

To get file size in PHP filesize() is use.
Syntax: filesize(“filename”);
This function take only 1 parameter that is file name (in string). This function will return file size .

To close file in PHP fclose() is use.
Syntax: fclose(file);
This function takes only 1 parameter that is file (file which you want to close).

PHP Code

print fread($file,filesize(“abc.txt”));

Text File (abc.txt)



Posted in: PHP

Leave a Reply