Giter Site home page Giter Site logo

filestream's Introduction

FileStream read files chunk by chunk

PHP class that can read large files (1GB, 5GB,20GB, 50GB) chunk by chunk

Examples

Read file using bytes where chunk size is bytes length

$stream = new FileStream( $input_file_path ); // String - path to file
$stream->chunkSize = 1024; // How many bytes to allocate in one call

foreach($stream->start() as $buffer){

  echo $buffer;

}

Read file line by line where chunk size is one line.

$stream = new FileStream( $input_file_path );
$stream->usingBytes = false; // Stream in lines

foreach($stream->start() as $line){ $line is object representing line content and number

  echo $line->number . " : " . $line->content . "\n";
  echo $stream->getMemoryUsage() . "\n";
}

Read file using lines and allocate more lines in one call

$stream = new FileStream( $input_file_path );
$stream->chunkSize = 50000; // Integer - How many lines to allocate in one call
$stream->usingBytes = false; // Stream in lines

foreach($stream->start() as $lines){ // $lines is now array of line objects with array size the same as chunk size

  foreach ($lines as $line) {
    file_put_contents('output_to_file.txt', $line->content, FILE_APPEND);
  }

}

Tests

During testing i have created dummy file (10000 lines) that you can use for testing and script in /tests
Here are results:

Chunk size 10000 (all lines allocated in memory in one call):
Line number: 1     Memory usage: 11.183MB
Line number: 1000  Memory usage: 11.183MB
Line number: 2000  Memory usage: 11.183MB
Line number: 5000  Memory usage: 11.183MB
Line number: 7000  Memory usage: 11.183MB
Line number: 10000 Memory usage: 11.183MB

Chunk size 10:
Line number: 1     Memory usage: 415.781KB
Line number: 1000  Memory usage: 415.820KB
Line number: 2000  Memory usage: 415.820KB
Line number: 5000  Memory usage: 415.820KB
Line number: 7000  Memory usage: 415.820KB
Line number: 10000 Memory usage: 415.820KB

filestream's People

Contributors

vojvodic avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.