Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»PHP»PHP Read from File – fread() Function

    PHP Read from File – fread() Function

    RahulBy RahulSeptember 4, 20161 Min ReadUpdated:September 6, 2016

    PHP fread() function is used for reading content from using php script. This function takes two arguments as described below.

    1. File specifies filename to read content
    2. Length specifies number of bytes to read from file.

    Syntax

    PHP fread() function uses following syntax.

      fread(file, length)
    

    Example

    Below is the two examples of reading content from file using php script.

    First example will read first 20 bytes from file.

    <?php
      $fn = fopen("myfile.txt","r");
      $result = fread($fn, 20);
      echo $result;
      fclose($fn);
    ?>
    

    Use this example to read all content from file. In this we used filesize() function to calculate size of file and passed it as argument for length.

    <?php
      $fn = fopen("myfile.txt","r");
      $result = fread($fn, filesize($fn));
      echo $result;
      fclose($fn);
    ?>
    
    fread PHP
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous ArticleRead File Line by Line – PHP fgets() Function
    Next Article PHP Read file Content into String – file_get_contents() Function

    Related Posts

    How to Install Composer on Ubuntu 22.04

    Updated:June 24, 20223 Mins Read

    How To Install PHP (8.1, 7.4 or 5.6) on Ubuntu 22.04

    Updated:June 19, 20223 Mins Read

    How To Install Linux, Nginx, MySQL, & PHP (LEMP Stack) on Ubuntu 22.04

    Updated:April 7, 20227 Mins Read

    How to Install Apache, MySQL, PHP (LAMP Stack) on Ubuntu 22.04

    Updated:July 28, 20225 Mins Read

    How To Setup Apache, PHP & MongoDB in Ubuntu & Debian

    Updated:October 8, 20213 Mins Read

    How To Install and Use PHP Composer on Debian 11

    Updated:February 16, 20224 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How To Install Google Chrome On macOS
    • How to Install Minecraft on Ubuntu 22.04 & 20.04
    • Running a Cron job every Sunday (Weekly)
    • Running Multiple Commands At Once in Linux
    • What are the Access Modifiers in Java
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.