PHPackages                             salvatorepandolfi/uploaded-file-type-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. salvatorepandolfi/uploaded-file-type-bundle

ActiveSymfony-bundle[File &amp; Storage](/categories/file-storage)

salvatorepandolfi/uploaded-file-type-bundle
===========================================

A Symfony bundle that handle the form FileType upload and store the URL for you

04PHP

Since May 25Pushed 2y agoCompare

[ Source](https://github.com/salvatorepandolfi/uploaded-file-type-bundle)[ Packagist](https://packagist.org/packages/salvatorepandolfi/uploaded-file-type-bundle)[ RSS](/packages/salvatorepandolfi-uploaded-file-type-bundle/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

UploadedFileType Bundle
=======================

[](#uploadedfiletype-bundle)

Don't handle the upload, storage, and access logic of your entities images ! Just point where you want to upload the file, and only store the URL of it.

Installation
============

[](#installation)

Make sure Composer is installed globally, as explained in the [installation chapter](https://getcomposer.org/doc/00-intro.md)of the Composer documentation.

Applications that use Symfony Flex
----------------------------------

[](#applications-that-use-symfony-flex)

Open a command console, enter your project directory and execute:

```
$ composer require tiloweb/uploaded-filetype-bundle
```

Applications that don't use Symfony Flex
----------------------------------------

[](#applications-that-dont-use-symfony-flex)

### Step 1: Download the Bundle

[](#step-1-download-the-bundle)

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

```
$ composer require tiloweb/uploaded-filetype-bundle
```

### Step 2: Enable the Bundle

[](#step-2-enable-the-bundle)

Then, enable the bundle by adding it to the list of registered bundles in the `config/bundles.php` file of your project:

```
// config/bundles.php

return [
    // ...
    Tiloweb\UploadedFileTypeBundle\UploadedFileTypeBundle::class => ['all' => true],
];
```

Configuration
=============

[](#configuration)

### Step 1 : Configure your filesystem

[](#step-1--configure-your-filesystem)

Use the [OneUp FlySystem](https://github.com/1up-lab/OneupFlysystemBundle) bundle to configure the filesystem you want to work with.

### Step 2 : create a default configuration

[](#step-2--create-a-default-configuration)

```
# config/package/uploaded_file_type.yml
uploaded_file_type:
  configurations:
    default:
      filesystem: 'oneup_flysystem.your_filesystem'
      base_uri: 'https://www.exemple.com/upload'
      path: '/image'
```

You can create many configurations, here, you will use the `default` configuration :

- `filesystem` : the alias to the OneUp FlySystem you want to use.
- `base_uri` : the URL to access to the root of your filesystem.
- `path` : The folder you want to upload your file to.

Usage
=====

[](#usage)

Simple as 🦆 !

Juste create a Form with a FileType field, with the option `upload` :

```
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('image', FileType::class, [
            'upload' => 'default'
        ])
    ;
}
```

When the form will be submited, the file will be uploaded by the filesystem of the `default` configuration, the URL of the file will be constructed and stored in your `$image` field.

You are able to change the naming strategy of your file once stored on your filesystem. To do so, you can add a `filename` option to your `FileType` pointing to an enclosure taking 2 parameters :

1. `UploadedFile $file` will contain the file uploaded by through form
2. `UploadedFile $item` will contain the data object of your form.

By default, the naming strategy is :

```
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('image', FileType::class, [
            'upload' => 'default',
            'filename' => function(UploadedFile $file, $item) {
                $filename = $file->getClientOriginalName();

                $filename = str_replace(
                    '.' . $file->guessClientExtension(),
                    '.' . md5(microtime().rand(0, 1000)) . '.' . $file->guessClientExtension(),
                    $filename
                );

                return $filename;
            }
        ])
    ;
}
```

Example
=======

[](#example)

Here, you will create a form in order to create a `Retail` entity with a logo that you want to store on the server.

```
