rcsofttech85/file-handler

一个用于抽象各种文件操作并提供文件操作辅助函数的简单库

1.5.5 2023-10-12 10:03 UTC

This package is auto-updated.

Last update: 2024-09-14 18:42:22 UTC


README

License Codacy Badge Codacy Badge

这是一个简单的PHP文件辅助库,用于各种文件操作。

目录

关于

这个PHP文件辅助库旨在简化各种文件相关操作。它提供了一系列功能,以处理诸如在文件中搜索关键字、将文件转换为不同格式、加密和解密文件等任务。无论您是处理CSV、JSON还是纯文本文件,这个库都可以简化您的文件管理流程。

安装

您可以通过Composer安装此PHP文件辅助库

composer require rcsofttech85/file-handler

用法

按关键字搜索

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

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


搜索并返回数组

$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


 ];

同时写入多个文件

$fileHandler = new FileHandler();

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

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

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

$fileHandler->close();

将文件转换为数组


$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'

        ];

在CSV文件中查找和替换


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

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

在CSV文件的特定列中查找和替换特定关键字


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

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

将文件转换为JSON格式


$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"}]

加密和解密文件


$secret = getenv('SECRET_KEY');

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

从URL流式传输并将内容保存到文件中


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

文件压缩和解压缩


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

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



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

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

文件差异

vendor/bin/file-diff oldFile newFile

文件完整性检查

$fileHasher = new FileHashChecker();

$fileHasher->hashFile(); 

$fileHasher->verifyHash($hashListFile);

在终端中查看CSV

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

在终端中查看JSON

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

对CSV文件应用过滤器

Csv File