banner



In Php, Writing Data To A File Requires How Many Steps?

PHP - Files & I/O


This chapter will explicate following functions related to files −

  • Opening a file
  • Reading a file
  • Writing a file
  • Closing a file

Opening and Closing Files

The PHP fopen() function is used to open a file. It requires two arguments stating first the file name and then style in which to operate.

Files modes can exist specified every bit one of the 6 options in this table.

Sr.No Mode & Purpose
one

r

Opens the file for reading simply.

Places the file arrow at the commencement of the file.

2

r+

Opens the file for reading and writing.

Places the file pointer at the start of the file.

3

w

Opens the file for writing only.

Places the file pointer at the showtime of the file.

and truncates the file to null length. If files does not

exist then it attempts to create a file.

4

due west+

Opens the file for reading and writing just.

Places the file pointer at the outset of the file.

and truncates the file to nada length. If files does non

be then it attempts to create a file.

5

a

Opens the file for writing but.

Places the file pointer at the end of the file.

If files does not exist and then information technology attempts to create a file.

half dozen

a+

Opens the file for reading and writing only.

Places the file pointer at the terminate of the file.

If files does not exist then it attempts to create a file.

If an try to open a file fails then fopen returns a value of faux otherwise information technology returns a file pointer which is used for further reading or writing to that file.

After making a changes to the opened file it is important to close it with the fclose() function. The fclose() function requires a file pointer as its argument and then returns true when the closure succeeds or false if it fails.

Reading a file

Once a file is opened using fopen() function it can exist read with a function called fread(). This office requires two arguments. These must be the file pointer and the length of the file expressed in bytes.

The files length can be found using the filesize() function which takes the file name as its argument and returns the size of the file expressed in bytes.

And then hither are the steps required to read a file with PHP.

  • Open a file using fopen() office.

  • Get the file's length using filesize() role.

  • Read the file's content using fread() function.

  • Close the file with fclose() function.

The post-obit instance assigns the content of a text file to a variable then displays those contents on the web folio.

<html>     <head>       <championship>Reading a file using PHP</title>    </caput>        <body>              <?php          $filename = "tmp.txt";          $file = fopen( $filename, "r" );                    if( $file == false ) {             echo ( "Error in opening file" );             exit();          }                    $filesize = filesize( $filename );          $filetext = fread( $file, $filesize );          fclose( $file );                    echo ( "File size : $filesize bytes" );          repeat ( "<pre>$filetext</pre>" );       ?>           </torso> </html>        

Information technology will produce the following consequence −

Reading File

Writing a file

A new file tin be written or text tin can exist appended to an existing file using the PHP fwrite() part. This function requires ii arguments specifying a file arrow and the string of data that is to be written. Optionally a third integer statement tin be included to specify the length of the data to write. If the third argument is included, writing would will stop after the specified length has been reached.

The post-obit example creates a new text file then writes a brusk text heading inside information technology. Later on closing this file its existence is confirmed using file_exist() function which takes file name as an argument

<?php    $filename = "/home/user/guest/newfile.txt";    $file = fopen( $filename, "westward" );        if( $file == false ) {       echo ( "Error in opening new file" );       exit();    }    fwrite( $file, "This is  a simple exam\n" );    fclose( $file ); ?> <html>        <caput>       <title>Writing a file using PHP</title>    </caput>        <body>              <?php          $filename = "newfile.txt";          $file = fopen( $filename, "r" );                    if( $file == false ) {             echo ( "Error in opening file" );             exit();          }                    $filesize = filesize( $filename );          $filetext = fread( $file, $filesize );                    fclose( $file );                    echo ( "File size : $filesize bytes" );          repeat ( "$filetext" );          echo("file name: $filename");       ?>           </body> </html>        

It will produce the following effect −

Writing File

We accept covered all the function related to file input and out in PHP File Organisation Function chapter.

Useful Video Courses

PHP Online Training

Video

Become PHP Facebook Developer: Password-Less Authentication

Video

Ultimate PHP &amp; MySQL Web Development Course &amp; OOP Coding

Video

Learn PHP from Scratch

Video

E Commerce Website Development In PHP With PDO

Video

PHP  in Telugu

Video

In Php, Writing Data To A File Requires How Many Steps?,

Source: https://www.tutorialspoint.com/php/php_files.htm

Posted by: grayvick1986.blogspot.com

0 Response to "In Php, Writing Data To A File Requires How Many Steps?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel