使用symfony上传PDF文件

时间:2017-03-24 14:23:12

标签: php symfony pdf

我需要有关如何在symfony中上传pdf文件的帮助。一般而言,学生与pdf卡之间的关系如下:单个学生可以为一个学生提供多张pdf卡和一张卡。实体表如下:

class FichePDF
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="Nom", type="string", length=255)
     */
    private $nom;

    /**
     * @ORM\Column(type="string")
     *
     * @Assert\NotBlank(message="Please, upload the evaluation file as a PDF file.")
     * @Assert\File(mimeTypes={ "application/pdf" })
     */
    private $file;

    /**
     * @var string
     *
     * @ORM\Column(name="Path", type="string", length=255)
     */


    private $path;

    /**
     * @ORM\ManyToOne(targetEntity="Polytech\SkillsBundle\Entity\Utilisateur", inversedBy="fichesPdf")
     * @ORM\JoinColumn(nullable=false)
     *
     */
    private $etudiant;

    /**
     * @ORM\OneToOne(targetEntity="Polytech\SkillsBundle\Entity\SousOccasion")
     * @ORM\JoinColumn(name="ssocc_id", referencedColumnName="id")
     */

    private $ssocc;

当然,有吸气剂和制定者。对于学生实体,我添加了这一行

/**
     * @ORM\OneToMany(targetEntity="Polytech\SkillsBundle\Entity\FichePDF" , mappedBy="etudiant", cascade={"remove"})
     */

    private $fichesPdf;

我有一个表格,可以检索我的应用程序中的几个实体的信息,例如教学单元,考试和学生,然后检索pdf文件。

<?php
namespace Polytech\SkillsBundle\Form\Rapport;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ButtonType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;

class FicheOccasionType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('ues', EntityType::class,
                array(
                    'class' => 'Polytech\SkillsBundle\Entity\UE',
                    'attr' => array('class' => 'browser-default ue'),
                    'choice_label' => 'nom',
                    'label' => false,
                    'required' => false,
                    'placeholder' => 'Choisissez une UE'
                )
            )
            ->add('etudiants', EntityType::class,
                array(
                    'class' => 'Polytech\SkillsBundle\Entity\Utilisateur',
                    'attr' => array('class' => 'browser-default etudiants'),
                    'choice_label' => 'nom',
                    'label' => false,
                    'required' => false,
                    'placeholder' => 'Choisissez un utilisateur'
                )
            )
            ->add('file', FileType::class, array('label' => 'PDF File'))
            ->add('submit', HiddenType::class)
            ->add('export', ButtonType::class, array('label' => 'Exporter'))
            ->add('import', ButtonType::class, array('label' => 'Import'));

    }

    public function getName()
    {
        return 'fiche_occasion';
    }
}

如何将文件检索为已上载文件并将其添加到数据库中。我阅读了文档,但这并不完全是我的工作。你能帮我吗

1 个答案:

答案 0 :(得分:2)

如文档中所述,您已经到了一半:

https://symfony.com/doc/current/controller/upload_file.html

您已经执行了以下步骤:

  1. 将属性添加到您的实体
  2. 将上传元素添加到表单
  3. 现在您必须处理上传的文件并将上传路径添加到实体。在您处理表单的控制器中,您现在必须执行以下操作:

    $fiche = new FichePDF();
    $form = $this->createForm(FichePDF::class, $fiche);
    $form->handleRequest($request);
    
    if ($form->isSubmitted() && $form->isValid()) {
        $file = $fiche->getFile();
        // Generate a unique name for the file before saving it
        $fileName = md5(uniqid()).'.'.$file->guessExtension();
        // Move the file to the directory where brochures are stored
        $file->move(
            $this->getParameter('upload_directory'),
            $fileName
        );
        // Update the 'fichePDF' property to store the PDF file name
        // instead of its contents
        $fiche->setFile($fileName);
    
        // Persist $fichePDF and do whatever else you want to
    }
    
相关问题