mycodebox/slim-file-response-plus

v1.0.2 2022-03-09 03:28 UTC

This package is auto-updated.

Last update: 2024-05-09 08:06:51 UTC


README

Slim Framework 3 File Response with Fallback Image or 404 Not Found

Install via composer

composer require mycodebox/slim-file-response-plus

Sample usage

<?php

// path returns  a PNG
$app->get('/test/image', function ($request, $response) {

    $filePath = $_SERVER["DOCUMENT_ROOT"]. "/path/to/your/image/file.png";

    return \MyCodebox\SlimFileResponse\FileResponse::getResponse($response, $filePath);
});

// path returns  a PDF file
$app->get('/test/pdf', function ($request, $response) {

    $filePath = $_SERVER["DOCUMENT_ROOT"]. "/path/to/your/pdf/file.pdf";

    return mhndev\slimFileResponse\FileResponse::getResponse($response, $filePath, 'myDocument');
});

// path returns  the given `filename` attribute
$app->get('/test/file/{filename}', function ($request, $response) {

    $fileName = $request->getAttribute('filename');
    $filePath = $_SERVER["DOCUMENT_ROOT"]. "/path/to/your/file/$fileName";

    return \MyCodebox\SlimFileResponse\FileResponse::getResponse($response, $filePath);
});

// path returns a `file_not_found.png` in case the given file is not found
$app->get('/test/file/{filename}', function ($request, $response) {

    $notFound = $_SERVER["DOCUMENT_ROOT"]. "/path/to/your/image/file_not_found.png";
    $fileName = $request->getAttribute('filename');
    $filePath = $_SERVER["DOCUMENT_ROOT"]. "/path/to/your/file/$fileName";
    $caching  = true; // default is false - withLastModified( filemtime(fileName) )

    return \MyCodebox\SlimFileResponse\FileResponse::getResponse($response, $filePath, null, $notFound, $caching);
});