PHP code example of sopinet / uploadfiles-bundle

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

    

sopinet / uploadfiles-bundle example snippets


  //app/AppKernel.php
  $bundles = array(
    ....
    new Sopinet\UploadFilesBundle\SopinetUploadFilesBundle(),
    new Oneup\UploaderBundle\OneupUploaderBundle(),
    ....
   )
  


/**
 * Example File Entity from https://github.com/sopinet/uploadfiles-bundle
 */
namespace AppBundle\Entity;

use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Bridge\Doctrine\Validator\Constraints as DoctrineAssert;


/**
 * @ORM\Entity
 * @ORM\Table(name="file")
 * @DoctrineAssert\UniqueEntity("id")
 * @ORM\HasLifecycleCallbacks
 */
class File
{
    use ORMBehaviors\Timestampable\Timestampable;

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $path;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return mixed
     */
    public function getPath()
    {
        return $this->path;
    }

    /**
     * @param $path
     * @return $this
     */
    public function setPath($path)
    {
        $this->path = $path;

        return $this;
    }


    public function getAbsolutePath()
    {
        return null === $this->path
            ? null
            : $this->getUploadRootDir().'/'.$this->path;
    }

    public function getWebPath()
    {
        return null === $this->path
            ? null
            : $this->getUploadDir().'/'.$this->path;
    }

    public function getFrontPath()
    {
        return null === $this->path
            ? null
            : $this->getUploadDir().'/'.$this->path;
    }

    protected function getUploadRootDir()
    {
        // the absolute directory path where uploaded
        // documents should be saved
        return __DIR__.'/../../../web'.$this->getUploadDir();
    }

    protected function getUploadDir()
    {
        // get rid of the __DIR__ so it doesn't screw up
        // when displaying uploaded doc/image in the view.
        return '';
    }

    public function getName()
    {
        return explode('/', $this->getPath())[2];
    }

    /**
     * @ORM\PostRemove
     */
    public function deleteFile(LifecycleEventArgs $event)
    {
        $fs = new Filesystem();
        //$fs->remove($this->getAbsolutePath());
    }
}

//src/AppBundle/Form/OilType.php
...
class OilType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
      $builder
        ->add('files', 'dropzone_file_gallery', array(
          'maxFiles'=> 4,//default 8
          '

//src/AppBundle/Form/OilType.php
...
class OilType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
      $builder
        ->add('files', 'dropzone_file_gallery', array(
          'maxFiles'=> 4,//default 8
          ' }}
            {%- endif -%}
            {{- block('form_rows') -}}
            {{- form_rest(form) -}}
        </div>
    </div>
{% endblock %}


//AppBundle/Admin/AcmeEntityAdmin.php
...
    /**
     * @param ListMapper $listMapper
     */
    protected function configureListFields(ListMapper $listMapper)
    {
        //...
        $listMapper
        ->add('files', 'dropzone_file_gallery', array(
            'template' => 'SopinetUploadFilesBundle:Admin:file_list.html.twig'
            )
        )

    }
...

//AppBundle/Admin/AcmeEntityAdmin.php
...
    public $fileUploadType='myblockName';
    /**
     * @param ListMapper $listMapper
     */
    protected function configureListFields(ListMapper $listMapper)
    {
        //...
        $listMapper
        ->add('file', 'dropzone_file_gallery', array(
            'template' => 'SopinetUploadFilesBundle:Admin:file.html.twig'
            )
        )

    }
...
//mytemplate.twig
...
{% block sonata_uploadfile_myblockName %}
    //your custom preview template here
{% endblock %
...

    //AppBundle/Admin/AcmeEntityAdmin.php
    ...
        public $yourFieldNameUploadType='pdf';
    ...