1. Go to this page and download the library: Download ho-nl-fork/psr7-csv-factory library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ho-nl-fork / psr7-csv-factory example snippets
use Psr\Http\Message\StreamInterface;
use Bakame\Psr7\Adapter\StreamWrapper;
public static StreamWrapper::streamToResource(StreamInterface $stream): resource;
public static StreamWrapper::streamToAppendableResource(StreamInterface $stream): resource;
use Bakame\Psr7\Adapter\StreamWrapper;
use League\Csv\Reader;
use League\Csv\Writer;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
final class CsvDelimiterSwitcherAction
{
public function __construct(
private string $inputDelimiter,
private string $outputDelimiter
) {
}
public function __invoke(ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
//let's create a CSV Reader object from the submitted file
$inputCsv = $request->getUploadedFiles()['csv'];
$reader = Reader::createFromStream(
StreamWrapper::streamToResource($inputCsv->getStream())
);
$reader->setDelimiter($inputDelimiter);
$psr7stream = $response->getBody();
$psr7stream->write($reader->getInputBOM());
//let's create a CSV Writer object from the response body
//because we already wrote to the stream we need to have an appendable resource
$writer = Writer::createFromStream(
StreamWrapper::streamToAppendableResource($psr7stream);
);
//we convert the delimiter
$writer->setDelimiter($outputDelimiter);
$writer->insertAll($reader);
//we add CSV header to enable downloading the converter document
return $response
->withHeader('Content-Type', 'text/csv, charset=utf-8')
->withHeader('Content-Transfer-Encoding', 'binary')
->withHeader('Content-Description', 'File Transfer')
->withHeader('Content-Disposition', 'filename=csv-'.(new DateTimeImmutable())->format('Ymdhis').'.csv')
;
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.