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

Related tags

Miscellaneous file
Overview

File

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

Supported PHP Versions

Build Status

(dev)

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.

Override 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 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 File

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.

You might also like...
 Learning about - Basic HTML & CSS, JSON, XML, Session & Cookies, CRUD Operations in Php using MySQL and Create MVC from scratch
Learning about - Basic HTML & CSS, JSON, XML, Session & Cookies, CRUD Operations in Php using MySQL and Create MVC from scratch

This Project is based on course CSC 3215. Learning about - Basic HTML & CSS, JSON, XML, Session & Cookies, CRUD Operations in Php using MySQL and Create MVC (Model–View–Controller) from scratch. Just learning about web technologies, Not focusing on UI (Bootstrap or other 3rd-Party UI libraries or frameworks).

Optimizes class loading performance by generating a single PHP file containing all of the autoloaded files.
Optimizes class loading performance by generating a single PHP file containing all of the autoloaded files.

Class Preloader for PHP This tool is used to generate a single PHP script containing all of the classes required for a specific use case. Using a sing

PHP library with basic objects and more for working with Facebook/Metas Conversions API

PHP library with basic objects and more for working with Facebook/Metas Conversions API Installation The easiest way to install this library is by ins

A status monitor for Elite Dangerous, written in PHP. Designed for 1080p screens in the four-panel-view in panel.php, and for 7 inch screens with a resolution of 1024x600 connected to a Raspberry Pi.

EDStatusPanel A status monitor for Elite Dangerous, written in PHP. Designed for 1080p screens in the four-panel-view in panel.php, and for 7 inch scr

Provide CSV, JSON, XML and YAML files as an Import Source for the Icinga Director and optionally ship hand-crafted additional Icinga2 config files
Provide CSV, JSON, XML and YAML files as an Import Source for the Icinga Director and optionally ship hand-crafted additional Icinga2 config files

Icinga Web 2 Fileshipper module The main purpose of this module is to extend Icinga Director using some of it's exported hooks. Based on them it offer

This is a simple PHP Student Portal. You can login, logout, register, and view your details.
This is a simple PHP Student Portal. You can login, logout, register, and view your details.

Student-Portal This is a simple PHP Student Portal with a MySQL Database. I made this as part of a university assignment, so you can login, logout, re

Type and shape system for arrays. Help write clearer code when implementing configs for your PocketMine-MP plugin or composer project.
Type and shape system for arrays. Help write clearer code when implementing configs for your PocketMine-MP plugin or composer project.

ConfigStruct Type and shape system for arrays. Help write clearer code when implementing configs for your PocketMine-MP plugin or composer project. It

Perch Dashboard app for exporting content to (Kirby) text files and Kirby Blueprint files
Perch Dashboard app for exporting content to (Kirby) text files and Kirby Blueprint files

toKirby Perch Dashboard app for exporting content to (Kirby) text files and Kirby Blueprint files. You can easily install and test it in a few steps.

ATVController for android devices running with RDM POGO ATLAS. Quick, simple and dirty add controls with a GUI view

ATVController - WIP - Much to add and fix. ATVController for android devices running with RDM POGO ATLAS. Quick, simple and dirty adb controls wit

Releases(v1.2.2)
  • v1.2.2(Nov 7, 2022)

    What's Changed

    • Fix to Bug in Ranges by @usernane in https://github.com/WebFiori/file/pull/9
    • Added More Validation Rules for Upload Path by @usernane in https://github.com/WebFiori/file/pull/10

    Full Changelog: https://github.com/WebFiori/file/compare/v1.2.1...v1.2.2

    Source code(tar.gz)
    Source code(zip)
  • v1.2.1(Jul 15, 2022)

    What's Changed

    • Dev by @usernane in https://github.com/WebFiori/file/pull/8

    Full Changelog: https://github.com/WebFiori/file/compare/v1.2.0...v1.2.1

    Source code(tar.gz)
    Source code(zip)
  • v1.2.0(Jul 15, 2022)

    What's Changed

    • Total Refactoring + Uploader by @usernane in https://github.com/WebFiori/file/pull/7

    Full Changelog: https://github.com/WebFiori/file/compare/v1.1.3...v1.2.0

    Source code(tar.gz)
    Source code(zip)
  • v1.1.3(Jun 26, 2022)

    What's Changed

    • Fix to a small bug by @usernane in https://github.com/WebFiori/file/pull/6

    Full Changelog: https://github.com/WebFiori/file/compare/v1.1.2...v1.1.3

    Source code(tar.gz)
    Source code(zip)
  • v1.1.2(Jun 22, 2022)

    What's Changed

    • Dev by @usernane in https://github.com/WebFiori/file/pull/5

    Full Changelog: https://github.com/WebFiori/file/compare/v1.1.1...v1.1.2

    Source code(tar.gz)
    Source code(zip)
  • v1.1.1(May 14, 2022)

  • v1.1.0(May 11, 2022)

    What's Changed

    • Changed How Encoding and Decoding Works by @usernane in https://github.com/WebFiori/file/pull/1
    • Changed How Files Will be Created by @usernane in https://github.com/WebFiori/file/pull/2
    • Added an Option to Create File Path if not Exist by @usernane in https://github.com/WebFiori/file/pull/3
    • Added Way to Store and Read Files in Base 64 by @usernane in https://github.com/WebFiori/file/pull/4

    New Contributors

    • @usernane made their first contribution in https://github.com/WebFiori/file/pull/1

    Full Changelog: https://github.com/WebFiori/file/compare/v1.0.0...v1.1.0

    Source code(tar.gz)
    Source code(zip)
  • v1.0.0(May 10, 2022)

Owner
WebFiori
WebFiori framework offical GitHub organization.
WebFiori
A PHP library to write values to .env (DotEnv) files

DotEnvWriter A PHP library to write values to .env (DotEnv) files Installation DotEnvWriter can be installed with Composer. composer require mirazmac/

Miraz Mac 9 May 24, 2022
Write to Laravel Config files and maintain file integrity

Laravel Config Writer Write to Laravel Config files and maintain file integrity. This library is an extension of the Config component used by Laravel.

Sam Geo 158 Dec 30, 2022
ICSGenerator - The module can generate basic ICS calendar strings and files

ICSGenerator The module can generate basic ICS calendar strings and files. The module simply extends WireData. It has these properties and default val

Timo Hausmann 4 Jun 25, 2022
StickWithIt is an online food ordering website created using PHP. You can view and purchase various items as well as remove items from the cart.

StickWithIt (App Name) StickWithIt is an online food ordering website created using PHP. The database used here is MYSQL database. The tool used here

Jenil Gajjar 1 May 11, 2022
High performance view templating API for PHP applications using tags & expressions inspired by Java JSTL and C compiler

View Language API Table of contents: About Expressions Tags Configuration Compilation Installation Unit Tests Examples Reference Guide About This API

Lucian Gabriel Popescu 0 Jan 9, 2022
Exploiting and fixing security vulnerabilities of an old version of E-Class. Project implemented as part of the class YS13 Cyber-Security.

Open eClass 2.3 Development of XSS, CSRF, SQLi, RFI attacks/defences of an older,vulnerable version of eclass. Project implemented as part of the clas

Aristi_Papastavrou 11 Apr 23, 2022
A PHP library to read and validate EU Digital COVID Certificates

CovPassCheck PHP A PHP library to read and validate EU Digital COVID Certificates. Install composer require stwon/covpasscheck-php Usage Currently, th

Studentenwerk OstNiedersachsen 6 Feb 7, 2022
🖍 Write beautiful blog articles using Markdown inside your Laravel app.

Blogged Write beautiful blog articles using Markdown inside your Laravel app. Blogged ?? Blogged is a carefully designed Laravel package provides an e

Saleem Hadad 131 Dec 16, 2022
Allows generate class files parse from json and map json to php object, including multi-level and complex objects;

nixihz/php-object Allows generate class files parse from json and map json to php object, including multi-level and complex objects; Installation You

zhixin 2 Sep 9, 2022
This PHP class uploads files and manipulates images very easily

This PHP class uploads files and manipulates images very easily. It is in fact as much as an image processing class than it is an upload class. Compatible with PHP4, 5 and 7. Supports processing of local files, uploaded files, files sent through XMLHttpRequest.

Colin Verot 795 Dec 21, 2022