PHP code example of netliva / symfony-filehelper

1. Go to this page and download the library: Download netliva/symfony-filehelper 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/ */

    

netliva / symfony-filehelper example snippets


// config/bundles.php
return [
    // ...
    Netliva\SymfonyFileHelperBundle\NetlivaSymfonyFileHelperBundle::class => ['all' => true],
];

use Netliva\SymfonyFileHelperBundle\Services\NetlivaFileHelper;
use Netliva\SymfonyFileHelperBundle\Services\NetlivaImageHelper;

class FileController extends AbstractController
{
    public function upload(
        NetlivaFileHelper $fileHelper,
        NetlivaImageHelper $imageHelper
    ) {
        // Dosya yolu alma
        $filePath = $fileHelper->getFilePath('user_photos', 'profile', $userId);
        
        // Görüntü işleme
        $thumbnail = $imageHelper->createThumbnail($filePath, 200, 200);
        
        // Güvenli URL oluşturma
        $secureUrl = $fileHelper->mediaSecureUri($filePath);
        
        return $this->json([
            'file_path' => $filePath,
            'secure_url' => $secureUrl,
            'thumbnail' => $thumbnail
        ]);
    }
}

use Netliva\SymfonyFileHelperBundle\Event\PublicUrlEvent;
use Netliva\SymfonyFileHelperBundle\Event\NetlivaFileHelperEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class FileHelperSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            NetlivaFileHelperEvents::PUBLIC_URL => 'onPublicUrl',
        ];
    }

    public function onPublicUrl(PublicUrlEvent $event): void
    {
        // URL'i özelleştir
        $path = $event->getPath();
        $customPath = '/custom/' . basename($path);
        $event->setPath($customPath);
    }
}

// Kullanıcı profil fotoğrafı
$fileHelper->getFilePath('user_photos', 'profile', $userId);

// Ürün görselleri
$fileHelper->getFilePath('products', 'main_image', $productId);

// Dokümanlar
$fileHelper->getFilePath('documents', 'invoice', $invoiceId);