Learn PHP File Operations : Fopen(), Fgets(), Feof(), Fread(), Fwrite(), Fputs() Function

Share:

Learn PHP File Operations : Fopen(), Fgets(), Feof(), Fread(), Fwrite(), Fputs() Function. All programming languages ​​provide facilities to access the file (file operations), is no exception with php. In php known several functions for file operations, among others, fopen, fgets, feof, fread, fwrite, fputs and so forth. Php file operations can serve to open and read the file, to write into a file with extension whatsoever.
Learn PHP File Operations : Fopen(), Fgets(), Feof(), Fread(), Fwrite(), Fputs() FunctionIn this tutorial we will learn it all. The following discussion :

Learn PHP File Operations : Fopen(), Fgets(), Feof(), Fread(), Fwrite(), Fputs() Function

PHP Open File
To open a file or url using fopen() function, here’s an example :

$file = fopen("example.txt", "r");

Fopen function requires two parameters. The first parameter is the name of the file to be opened, in the above example the file name “example.txt”. The second parameter is the goal of opening the file, the options are :

  • “r” for read (read)
  • “w” to write (write)
  • “a” to append (add a data file on the last line).
  • In the parameter “w” if the file in question is not yet available (no) then the file will be created, but if the file exists then the old file will be overwritten by the new file. When you have finished editing the file, the function fopen() needs to be closed with fclose() as follows :

    $file = fopen("example.txt", "w");
    fclose($file);
    

    PHP Read File
    To read a file line by line using the function fgets(). Prior to reading the file, first file is opened using fopen() the following example :

    $file = fopen("example.txt","r");
    $read = fgets($file, 1024);
    

    Here is a sample program that uses fgets() :

    <?php
    $file = fopen("example.txt","r");
    while (!feof($file))
    {
    $read = fgets($file, 1024);
    echo "$read <br>";
    }
    fclose($file);
    ?>
    

    The function feof() like the above program is used to check the last line of a file. Besides fgets() no other way to read a file that is the function fread() The following is a sample program that uses fread() :

    <?php
    $file = fopen("example.txt","r");
    while (!feof($file))
    {
    $read = fread($file, 16);
    echo "$read <br>";
    }
    fclose($file);
    ?>
    

    PHP Write File
    To write to a file using the function fputs(). Just like when reading the file, before calling the function fputs() file must first be opened using fopen(). If you intend to create a new file or overwrite the old files with the new files then use the parameter “w” on the fopen(), but if you want to add data from an existing file then use the parameter “a” (append). Here is a sample program that uses fputs() to write to the file.

    <?php
    $file = fopen("example.txt","a");
    fputs($file, "I like learn PHP programming");
    fclose($file);
    ?>
    

    Besides fputs() function, you can also use the fwrite() function to write to a file, there is no significant difference of the this function, the same form of the writing. Here’s an example :

    fwrite ($ file, "I like learn PHP programming");

    Actually there are other functions related to the operation of the php file, we will probably continue in the coming tutorial post.
    Thanks for reading Learn PHP File Operations : Fopen(), Fgets(), Feof(), Fread(), Fwrite(), Fputs() Function

    [Tutorial.World.Edu]

    One thought on “Learn PHP File Operations : Fopen(), Fgets(), Feof(), Fread(), Fwrite(), Fputs() Function

    Leave a Reply

    Your email address will not be published. Required fields are marked *