webfiori/file

Basic class library to read, write and view files using PHP.

v1.3.6 2024-04-29 20:08 UTC

This package is auto-updated.

Last update: 2024-04-29 20:10:36 UTC


README

Basic class library to read, write and view files using PHP.

badge.svg?branch=main 68747470733a2f2f636f6465636f762e696f2f67682f57656246696f72692f66696c652f6272616e63682f6d61696e2f67726170682f62616467652e737667 68747470733a2f2f736f6e6172636c6f75642e696f2f6170692f70726f6a6563745f6261646765732f6d6561737572653f70726f6a6563743d57656246696f72695f66696c65266d65747269633d616c6572745f737461747573 68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f77656266696f72692f66696c653f636f6c6f723d6c696768742d677265656e

Content

Supported PHP Versions

Build Status
php70.yml?branch=main
badge.svg?branch=main
badge.svg?branch=main
badge.svg?branch=main
badge.svg?branch=main
badge.svg?branch=main
badge.svg?branch=main
badge.svg?branch=main
badge.svg?branch=main

Main Aim of The Library

The main aim of the library is to have an OOP abstraction that simplifies most common operations with files in PHP.

Usage

Reading a File

$file = new File('path/to/my/file.txt');
$file->read();

$fileData = $file->getRawData();
// Do anything you like with data string.

Also, it is possible to read a specific range of bytes by supplying the range to the method File::read()

$file = new File('path/to/my/file.txt');
//Read bytes 10-100 inclusive
$file->read(10, 100);

$fileData = $file->getRawData();

Creating New File

$file = new File('path/to/my/new/file.txt');

//The method File::create() is used to create new files
$file->create();
$file->setRawData('Hello World');
$file->write();

Appending to Existing File

$file = new File('path/to/my/old/file.txt');
$file->setRawData('Hello');
$file->write();
$file->setRawData(' World');
$file->write();

//Setting raw data before each call to the method File::write() will append to file.

Overriding a File

$file = new File('path/to/my/old/file.txt');
$file->setRawData('Hello');
$file->write();
$file->setRawData(' World');
$file->write(true);

//By supplying true as parameter to the method File::write(), the old content of the file will be overridden. 

Encoding or Decoding of Files

Base64 encoding and decoding is usually used to make sure that binary data is stored and transmitted reliably from one place to another. For more information, read here

Decoding

$file = new File('file.txt');

//'Hello World!' in base64
$encodedData = 'SGVsbG8gV29ybGQh';

$file->setRawData($encodedData, true);
$decoded = $file->getRawData();

//$decoded is now the string 'Hello World!'
//By supplying true as second parameter to the method File::setRawData(), the method will decode given data
$file = new File('file.txt');
$file->setRawData('Hello World');

$encoded = $file->getRawData(true);
//$encoded is now the string 'SGVsbG8gV29ybGQh'
//By supplying true as second parameter to the method File::getRawData(), the method will encode given data

Reading and Storing Encoded Files

The method File::writeEncoded() is used to write base 64 enceded binary files as follows.

$file = new File('my-img.png');
$file->writeEncoded();

//This will create new file with the name 'my-img.png.bin'

The method File::readDecoded() is used to read base 64 enceded binary files as follows.

$file = new File('some-binary-with-encoded.bin');
$file->readDecoded();
$decoded = $file->getRawData();

Display a File

The method File::view() is used to dispatch the content of the file to front-end. It also supports the header content-range which can be used to get partial file content.

$file = new File('some-pdf.pdf');
$file->read();
$file->view();

To trigger a download dialog in web browser, supply true as argument to the method File::view().

$file = new File('some-pdf.pdf');
$file->read();
$file->view(true);

Chunking Files

Usually, when a big file is stored in a database table, it is encoded then divided into smaller chunks, and each chunk is stored in a record. The class provides a single method for doing such procedure.

$file = new File('some-big-movie-file.mp4');
$file->read();

//The size of each chunk will be 1500 bytes and they will be base 64 encoded by default.
$dataChunks = $file->getChunks(1500);

foreach ($dataChunks as $chunk) {
    //......
}

Supplying false as second parameter to the method will disable base 64 encoding.