close[x]


PHP

PHP-Home PHP-Environment Setup PHP-Syntax PHP-Run PHP in XAMPP PHP-Variable PHP-Comment PHP-Datatype PHP-String PHP-Operators PHP-Decision PHP-loop PHP-Get/Post PHP-Do While loop PHP-While loop PHP-For loop PHP-Foreach loop PHP-Array PHP-Multidimensional Arrays PHP-Associative Arrays PHP-Indexed Arrays PHP-Function PHP-Cookies. PHP-Session PHP-File upload PHP-Email PHP-Data & Time PHP-Include & Require PHP-Error PHP-File I/O PHP-Read File PHP-Write File PHP-Append & Delete File PHP-Filter PHP-Form Validation PHP-MySQl PHP-XML PHP-AJAX



learncodehere.com




PHP - Read File

PHP provides various functions to read data from file.

The available PHP file read functions are given below.

  • fread()
  • fgets()
  • fgetc()

  • First create a text file with file1.txt file name and put this content in the file.

    
                        Code Now
                        Code Tomorrow
                        Code Always
    

    PHP - fread()

    The PHP fread() function is used to read data of the file. It requires two arguments: file resource and file size.

    Syntax : Read File

    
     string fread (resource $handle , int $length )  
    

    $handle represents file pointer that is created by fopen() function.

    $length represents length of byte to be read.

    Lets put this into example.

    Example : Read File

    
    <?php  
    $myfile = "c:\\file1.txt"; 
    //open file in read mode      
    $fp = fopen($myfile, "r");    
    //read file    
    $contents = fread($fp, filesize($myfile));   
    //printing data of file
    echo "<pre>$contents</pre>";  
    fclose($fp);//close file
     ?> 

    This will produce the following result −

    Result

    
     Code Now
     Code Tomorrow
     Code Always  


    PHP - fgets()

    The PHP fgets() function is used to read single line from the file.

    Syntax : Read File

    
     string fgets ( resource $handle [, int $length ] )   
    

    $handle represents file pointer that is created by fopen() function.

    $length represents length of byte to be read.

    Lets put this into example.

    Example : Read File

    
    <?php
    //open file in read mode    
     $fp = fopen("c:\\file1.txt", "r");  
    echo fgets($fp);  
    fclose($fp);  
     ?> 

    This will produce the following result −

    Result

    
    Code Now
    



    PHP - fgetc()

    The PHP fgetc() function is used to read single character from the file.

    To get all data using fgetc() function, use !feof() function inside the while loop.

    Syntax : Read File

    
     string fgetc ( resource $handle )    
    

    $handle represents file pointer that is created by fopen() function.

    Lets put this into example.

    Example : Read File

    
    <?php  
    $fp = fopen("c:\\file1.txt", "r");  //open file in read mode    
    while(!feof($fp)) {  
      echo fgetc($fp);  
    }  
    fclose($fp);  
    ?>
    

    This will produce the following result −

    Result

    
    Code Now Code Tomorrow Code Always