File and Directory Functions #1 (fopen, fgets, fwrite, fputs, fread)

in #utopian-io8 years ago (edited)

What Will I Learn?

  • fopen Function
  • fgets Function
  • fwrite Function
  • fputs Function
  • fread Function

Requirements

  • Sublimetext / Notepad / Notepad++
  • localhost or php hosting

Difficulty

  • Intermediate

phps.png

File and Directory Functions

PHP provides us with powerful tools for working with files and directories as well as in other programming languages. Programmers apply files and directories very often. For example, it is possible to give examples such as the automatic creation of an HTML page in consideration of certain patterns, or the creation and use of classical file type databases, or the creation of an online file and directory management program, making various arrangements on files and directories.

fopen() Function

Open the file we want by specifying the file name and mode type for the fopen() function. It also opens and reads not only local files, but also files associated with a site address, for example. The fopen() function is used as "fopen (filename, mode)". If the file name starts with "HTTP: //", the HTTP 1.0 connection to the specified server is opened. And the file variable returns the initial text of the response.
If the file starts with "FTP: //", an FTP connection to the specified server is opened and the file pointed to the given file. If the opening is not successful PHP will return a "false" response.
Opening a file with PHP, we perform read, write and append operations with the following mode.

r => It opens the file for reading.
Example:

fopen("File.txt","r");

r+ =>The file opens for reading and writing.
Example:

fopen("File.txt","r+" );

w => He writes on the file. Creates the file first if it does not exist.
Example:

fopen( "File.txt","w");

W+ => Opens for reading and overwriting files.
Example:

fopen("File.txt","w+");

a =>Makes additions to the contents in the file. Creates a file if it does not exist.
Example:

fopen("File.txt","a" );

a+ => Opens the file for reading and adding.

Example:

fopen("File.txt","a+");

b => Binary is used to save or read the file.

Example:

fopen("File.txt","rb");

Let's start the file operations by opening and reading a page through HTTP in the first example.

$File=fopen("Example.html","r"); 
while (!feof($File)) {

    $Read= fgets($File, 500);
    echo $Read;

}
fclose($File); 
Explanation:

opens the Example.html file to read. The loop continues until the end of the file.
500 bytes of data are read from the file. The results are printed on the screen. The file is closed.

In this example, you may be wondering what three file functions we have learned yet and what they do. These functions are explained in the subject, but I will explain it once again here, for example, in order to facilitate our work in the following stages.

The feof() function is used to see if we have come to the end of the data in a file. Located to the left of this function! sign. So while in the while loop, if the file is not at the end, the loop will continue.

The fgets() function allows you to read data at a desired size. In this example, 500 bytes of data are read during each cycle. Instead of 500 we can give a dimension we want. fclose() Closes an open file.

fgets() Function

Syntax:

fgets($FileVariable, $length)

It is a function we use when reading the data in the file. This function reads the file from a file at a desired size along with the file open variant. The implementation of the function depends on two variables. As you can see from the above examples, we open a file to open a variable, and after changing this variable as the first value of the fgets() function, we give the value of a dimension we want as a second value and read the data from the file.

Sample:
Open Notepad and create a counter.txt file as follows. Edit the following PHP code as php and save it as the counter.txt file. If both files are using Apache Web Server, you need to save them in htdocs directory.

Screenshot_1.png

    $File = fopen("counter.txt", "r"); 
    $Count = fgets($File, 10);
    fclose($File); 
    $Count++;
    echo $Count;
Explanation:

Open to read the file. Read 10 bytes of data from file with fgets() function. Close the file with the fclose() function. Increase the value of $Count variable by one.
Write the value of an incremented $Count variable to the screen.

On the screen will writes 897867.

If the information in the counter.txt file had been more than 10 characters, the reading would only cover the first 10 characters.

fwrite() Function

Syntax:

fwrite($FileVariable, $String, $Length)

It is a function we use to print data into a file. This function refers to three values. File size, the type of information to write, and the size of the information to be written. If the write to the file occurs, 1 is TRUE, otherwise it is FALSE.

    $Text ="I have a very bad exam today";
    $File = fopen("Example.txt","w"); 
    fwrite ($File, $Text, 13); 
    fclose($File); 

    $File = fopen("Example.txt","r"); 
    $Read = fgets($File, 30);
    fclose($File); 
    echo $Read;

The screen writes "I have a very bad exam today".
In our example, up to 13 characters (13 bytes) are written to the file with fwrite() function. If we do a 30-character reading from the file with the fgets() function, the screen says "I have a very bad exam today". Because there was only this sentence in the file.

fputs() Function

Syntax:

fputs($FileVariable, $String, $Length) 

Just like the fwrite function, it is used to print the data we want to the file. Let's give a counter example. Take 99 digits in the counter.txt file we created earlier. Open and read this file and increase the number in it, and let's print the last value obtained from this file again.

    $File = fopen("sayac.txt","r"); 
    $Read = fgets($File, 10);
    fclose($File); 
    $Read++;

    $File = fopen("sayac.txt","w"); 
    fputs($File, $Read); 
    fclose($File); 

    echo $Read;
Explanation:
  • Counter opens the file for reading.
  • Reads the first 10 characters in the file.
  • The file is closed. The value read from the file is incremented by 1.
  • Counter opens to overwrite file.
  • The value is incremented by 1.
  • The file opened for overwriting is closed.
  • An incremental number obtained from the file and written to the screen.

The screen will write 100.

fread() Function

Syntax:

fread($FileVariable, $Length)

It is a file read function.

The contents of the data.txt file should look like this

02.14.2018 Sample Writing #1
02.15.2018 Sample Writing #2 
02.16.2018 Sample Writing #3 
02.17.2018 Sample Writing #4 
02.18.2018 Sample Writing #5
    $Data = 'data.txt';
    $File =fopen($Data,"r"); 
    $Read = fread($File, fileSize($Data)); 
    fclose($File); 
    echo $Read; 

The filesize() function we use with the fread() function in the above code finds the size of any file. In this example, the intent of the filesize() function is to use the fread() function to read the information in the file at a glance. If you have not used the filesize() function, you can read the file by typing in a desired size instead of this function. The reading of the file will be as much as the size we gave it this time. If we want to read the entire file with the fread() function at a size we want, then we can use the while loop.

Screenshot_2.png

    $Data= 'data.txt';
    $File =fopen($Data,"r"); 
    $Read = fread($File, 10); 
    fclose($File); 
    echo $Read;
Explanation

Only the first 10 characters are read from the file.

    $Data= 'data.txt';
    $File =fopen($Data,"r"); 
    while ($Read = fread($File, 100)) {

        echo $Read;

    }
    fclose($File); 
Explanation:

100 bytes are read from the file each time while cycle.
All of the data obtained with the While loop is printed on the screen.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

@bosluk, Upvote is the only thing I can support you.

Congratulations, you were selected for a random upvote! Follow @resteemy and upvote this post to increase your chance of being upvoted again!
Read more about @resteemy here.

Your contribution cannot be approved because it does not follow the Utopian Rules.

  • Tutorials must be technical instructions that teach non-trivial aspects of an Open Source project.

  • Tutorials with only usage of some basic functions are not approved. There are already well documented explanations for those functions and the tutorials doesn't really add value to the community.

You can contact us on Discord.
[utopian-moderator]

Congratulations @bosluk! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

Click here to view your Board

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @bosluk! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.09
TRX 0.32
JST 0.034
BTC 108071.35
ETH 3862.24
USDT 1.00
SBD 0.63