Giter Site home page Giter Site logo

rcsofttech85 / php-file-helper Goto Github PK

View Code? Open in Web Editor NEW
18.0 1.0 3.0 216 KB

PHP File Handler Library is a versatile toolset designed to simplify file management tasks in your PHP projects.

PHP 100.00%
cli-utilities file-compression file-encryption file-integrity-check file-search file-streaming file-array-conversion file-difference-checker

php-file-helper's Introduction

PHP File Helper

License Codacy Badge Codacy Badge

A simple PHP file helper for various file operations.


Table of Contents

About

This PHP File Helper is designed to simplify various file-related operations. It offers a range of features to handle tasks such as searching for keywords in files, converting files to different formats, encrypting and decrypting files, and more. Whether you're working with CSV, JSON, or plain text files, this library can streamline your file management processes.

Installation

You can install this PHP File Helper library via Composer:

composer require rcsofttech85/file-handler

Usage

search by keyword

     $temp = new TempFileHandler();
     $csv = new CsvFileHandler($temp);

     $findByKeyword = $csv->searchInCsvFile("movies.csv","Twilight","Film");


Search and return array

$temp = new TempFileHandler();
$csv = new CsvFileHandler($temp);

$findByKeyword = $csv->searchInCsvFile("movies.csv","Twilight","Film",FileHandler::ARRAY_FORMAT);

// output

[
    [Film] => Twilight
    [Genre] => Romance
    [Lead Studio] => Summit
    [Audience score %] => 82
    [Profitability] => 10.18002703
    [Rotten Tomatoes %] => 49
    [Worldwide Gross] => $376.66 
    [Year] => 2008


 ];

Write multiple files simultaneously

$fileHandler = new FileHandler();

$fileHandler->open('file.txt');

$fileHandler->open('php://stdout');

$fileHandler->write(data: "hello world");

$fileHandler->close();

Converting file to an array


$temp = new TempFileHandler();
$csv = new CsvFileHandler($temp);

$findByKeyword = $csv->toArray("movies.csv");
// output
$data[0] = [
            'Film' => 'Zack and Miri Make a Porno',
            'Genre' => 'Romance',
            'Lead Studio' => 'The Weinstein Company',
            'Audience score %' => '70',
            'Profitability' => '1.747541667',
            'Rotten Tomatoes %' => '64',
            'Worldwide Gross' => '$41.94 ',
            'Year' => '2008'

        ];

Find and replace in csv file


$temp = new TempFileHandler();
$csv = new CsvFileHandler($temp);

$findByKeyword = $csv->findAndReplaceInCsv("movies.csv","Twilight","Inception");

Find and replace a specific keyword in a particular column of a CSV file


$temp = new TempFileHandler();
$csv = new CsvFileHandler($temp);

$findByKeyword = $csv->findAndReplaceInCsv("movies.csv","Inception","Twilight",column: "Film");

Converting file to json format


$temp = new TempFileHandler();
$csv = new CsvFileHandler($temp);

$findByKeyword = $csv->toJson("movies.csv");

//output
[{"Film":"Zack and Miri Make a Porno","Genre":"Romance","Lead Studio":"The Weinstein Company","Audience score %":"70","Profitability":"1.747541667","Rotten Tomatoes %":"64","Worldwide Gross":"$41.94 ","Year":"2008"},{"Film":"Youth in Revolt","Genre":"Comedy","Lead Studio":"The Weinstein Company","Audience score %":"52","Profitability":"1.09","Rotten Tomatoes %":"68","Worldwide Gross":"$19.62 ","Year":"2010"},{"Film":"Twilight","Genre":"Romance","Lead Studio":"Independent","Audience score %":"68","Profitability":"6.383363636","Rotten Tomatoes %":"26","Worldwide Gross":"$702.17 ","Year":"2011"}]

Encrypt and decrypt files


$secret = getenv('SECRET_KEY');

$fileEncryptor = new FileEncryptor('movie.csv', $secret);
$fileEncryptor->encryptFile();
$fileEncryptor->decryptFile();

Stream and save content from url to file


 $url = "https://gist.github.com/rcsofttech85/629b37d483c4796db7bdcb3704067631#file-gistfile1-txt";
 $stream = new Stream($url, "outputFile.html");
 $stream->startStreaming();

File compression and decompression


        $testFile = 'movie.csv';
        $compressedZipFilename = 'compressed.zip';

        $this->fileHandler->compress($testFile, $compressedZipFilename);



        $compressedZipFilename = 'compressed.zip';
        $extractPath = 'extracted_contents';

        $this->fileHandler->decompress($compressedZipFilename, $extractPath);

File Difference

vendor/bin/file-diff oldFile newFile

File Integrity Check

$fileHasher = new FileHashChecker();

$fileHasher->hashFile(); 

$fileHasher->verifyHash($hashListFile);

View csv in terminal

vendor/bin/view-csv movies.csv --hide-column Film --limit 5

View json in terminal

vendor/bin/view-json movies.json --hide-column Film --limit 5

Applying filters to a csv file

Csv File

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.