Skip to main content
Version: 2.1

Upload

To upload files there is the UploaderInterface. The UploaderInterface just returns an instance of File that provides a path for the file.

Config

Config NameExampleRequiredDescription
providers3trueThe mailing service to be used. Options are: null, s3, local
naming_strategyrandom_timetrue
s3n/afalseThe S3 options as defined in the AWS S3 section below.
localn/afalseThe local options as defined in the local section below.
urlhttps://public.url/trueThe base url the file will be located at.

Fetching an Uploader

To get the uploader you can just autowire Parthenon\Common\Upload\UploaderInterface. And that will inject either the uploader called default or if there is only one it'll inject that.

{{% notice info %}} If you have more than one uploader and neither are called default. Autowiring will result in an exception for no found uploader being flung. {{% /notice %}}

The other way to get the uploader is to call the Parthenon\Common\Upload\UploaderManager::getUploader. This can be injected via autowiring with the interface Parthenon\Common\Upload\UploaderManagerInterface.

use Parthenon\Common\Upload\UploadManager;

class Controller {
public function uploadAction(Request $request, UploaderManagerInterface $uploaderManager) {
$uploader = $uploaderManager->getUploader('profile_images');
// do stuff with uploader
}
}

Creating your own uploader

namespace Parthenon\Common\Upload;

use Symfony\Component\HttpFoundation\File\UploadedFile;

interface UploaderInterface
{

public function uploadString(string $filename, string $contents): File;

public function uploadUploadedFile(UploadedFile $file): File;

public function deleteFile(File $file): void;

/**
* @return resource
*/
public function readFile(File $file);
}

You need to implement three methods, Upload that accepts a Symfony UploadedFile, a delete, and a read both of which accept the Parthenon File object.

uploadString

This accepts a string for the filename and a string for the contents instance of File

uploadUploadedFile

This accepts an instance of UploadedFile and returns an instance of File

deleteFile

This accepts an instance of File and returns nothing

readFile

This accepts an instance of File and returns a stream.

Naming

Often there is a need for files to be named in a specific way.

namespace Parthenon\Common\Upload;

interface NamingStrategyInterface
{
public function getName(string $filename): string;
}

Strategies

NameDescription
md5_timeThis creates a md5 hash from the file name and then suffixes a unix timestamp to the end. This does result in a guessable filename.
random_timeThis this creates a random string that has the timestamp suffixed to it.

Flysystem

Flysystem is an extremely good uploading library that can be used for upload files to multiple systems. There is a wrapper for his library to allow use while also allowing for potential competitors in the future to be usable.

AWS S3

Config NameExampleRequiredDescription
keys3trueYour client key to AWS
secretrandom_timetrueYour secret for AWS
regionfra1trueThe name of the region you're going to upload to
endpointhttps://fra1.digitaloceanspaces.comtrueThe url to the AWS APIs.
bucket_namebucket_nametruethe name of the bucket you're uploading to.
visibilitypublicfalseWether or not you want the file to be publically accessible or not
parthenon:
common:
uploader:
default:
provider: s3
naming_strategy: random_time
url: '%env(resolve:UPLOAD_ACCESS_URL)%'
s3:
key: '%env(resolve:DIGITALOCEAN_SPACES_KEY)%'
secret: '%env(resolve:DIGITALOCEAN_SPACES_SECRET)%'
region: '%env(resolve:DIGITALOCEAN_SPACES_REGION)%'
endpoint: '%env(resolve:DIGITALOCEAN_SPACES_ENDPOINT)%'
bucket_name: '%env(resolve:DIGITALOCEAN_SPACES_BUCKET)%'
visibility: 'public'

Local

To upload to the local file system.

Config NameExampleRequiredDescription
path/tmptrueThe location on your local filesystem to store the file.
parthenon:
common:
uploader:
default:
provider: local
naming_strategy: time_random
local:
path: "%kernel.project_dir%/public/uploads"