PHP code example of mulertech / file-bundle

1. Go to this page and download the library: Download mulertech/file-bundle 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/ */

    

mulertech / file-bundle example snippets


namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use MulerTech\FileBundle\Entity\AbstractFile;

#[ORM\Entity]
#[ORM\Table(name: 'files')]
class File extends AbstractFile
{
    #[ORM\ManyToOne]
    #[ORM\JoinColumn(nullable: false)]
    private User $uploadedBy;

    // Add your own relations and fields...
}

use MulerTech\FileBundle\Model\FileUploaderInterface;

class User implements FileUploaderInterface
{
    public function getId(): ?int { /* ... */ }
}

use MulerTech\FileBundle\Service\FileManagerInterface;

class FileController
{
    public function upload(FileManagerInterface $fileManager, Request $request): Response
    {
        $uploadedFile = $request->files->get('file');
        $file = $fileManager->upload($uploadedFile, $user);

        // $file is your concrete entity, persisted and flushed
    }
}

use MulerTech\FileBundle\Storage\DirectoryStrategyInterface;
use MulerTech\FileBundle\Model\FileUploaderInterface;

class ProjectDirectoryStrategy implements DirectoryStrategyInterface
{
    public function getRelativeDirectory(FileUploaderInterface $uploader, ?object $context = null): string
    {
        if ($context instanceof Project) {
            return sprintf('project_%d', $context->getId());
        }

        return sprintf('user_%d', $uploader->getId());
    }
}