上传图片,将“filename”列设置为正在上传的文件的名称?

时间:2014-11-17 17:53:20

标签: php symfony file-upload yaml

我使用的是Sonata Admin套装,基本上,当用户上传轮播图片时(我有一个上传小部件),我想要"文件名"我的表的列将自动填入文件名。所以如果它," image_001.jpg"我希望文件名列自动说" image_001.jpg"文件上传后。我将如何实现这一目标?

另外,如果你有任何关于我能做的更好的提示,那就太棒了!我觉得我的桌子也应该有一个网址列,但我不确定旋转木马的图像是否会一直上传到下面代码中的路径。

这是我的YAML文件:

Example\CmsBundle\Entity\Carousel:
   type: entity
   table: carousel
   id:
      id:
         type: integer
         generator: { strategy: AUTO }
   fields:
      filename:
         type: string
         length: 100
      updated:
         type: datetime
         nullable: true
   lifecycleCallbacks:
      prePersist: [ lifecycleFileUpload ]
      preUpdate:  [ lifecycleFileUpload ]

我的实体档案:

/**
 * Carousel
 */
class Carousel
{

const SERVER_PATH_TO_IMAGE_FOLDER = 'bundles/examplecompany/images/carousel';

    /**
     * @var integer
     */
    private $id;

    /**
     * @var stringß
     */
    private $filename;

    /**
     * @var \DateTime
     */
    private $updated;

    /**
     * Unmapped property to handle file uploads
     */
    private $file;

    /**
     * Sets file.
     *
     * @param UploadedFile $file
     */
    public function setFile(UploadedFile $file = null)
    {
        $this->file = $file;
    }

    /**
     * Get file.
     *
     * @return UploadedFile
     */
    public function getFile()
    {
        return $this->file;
    }

    /**
     * Manages the copying of the file to the relevant place on the server
     */
    public function upload()
    {
        // the file property can be empty if the field is not required
        if (null === $this->getFile()) {
            return;
        }

        // we use the original file name here but you should
        // sanitize it at least to avoid any security issues

        // move takes the target directory and target filename as params
        $this->getFile()->move(
            Carousel::SERVER_PATH_TO_IMAGE_FOLDER,
            $this->getFile()->getClientOriginalName()
        );

        // set the path property to the filename where you've saved the file
        $this->filename = $this->getFile()->getClientOriginalName();

        // clean up the file property as you won't need it anymore
        $this->setFile(null);
    }

    /**
     * Lifecycle callback to upload the file to the server
     */
    public function lifecycleFileUpload() {
        $this->upload();
    }

    /**
     * Updates the hash value to force the preUpdate and postUpdate events to fire
     */
    public function refreshUpdated() {
        $this->setUpdated(new \DateTime("now"));
    }

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

    /**
     * Set filename
     *
     * @param string $filename
     * @return Carousel
     */
    public function setFilename($filename)
    {
        $this->filename = $filename;

        return $this;
    }

    /**
     * Get filename
     *
     * @return string
     */
    public function getFilename()
    {
        return $this->filename;
    }

    /**
     * Set updated
     *
     * @param \DateTime $updated
     * @return Carousel
     */
    public function setUpdated($updated)
    {
        $this->updated = $updated;

        return $this;
    }

    /**
     * Get updated
     *
     * @return \DateTime
     */
    public function getUpdated()
    {
        return $this->updated;
    }
    /**
     * @ORM\PrePersist
     */
    /*public function lifecycleFileUpload()
    {
        // Add your code here
    }*/
}

编辑:

轮播管理员代码:

class CarouselAdmin extends Admin
{

   // setup the default sort column and order
   protected $datagridValues = array(
      '_sort_order' => 'ASC',
      '_sort_by' => 'updated_at'
   );

   protected function configureFormFields(FormMapper $formMapper)
   {
      $formMapper
         ->add('file', 'file', array('required' => false))
         ->add('filename')
         ->add('text_block')
      ;
   }

   protected function configureDatagridFilters(DatagridMapper $datagridMapper)
   {
      $datagridMapper
         ->add('filename')
         ->add('updated_at')
      ;
   }

   protected function configureListFields(ListMapper $listMapper)
   {
      $listMapper
         ->add('')
         ->addIdentifier('filename')
         ->add('text_block')
         ->add('updated_at')
         // add custom action links
         ->add('_action', 'actions', array(
             'actions' => array(
                 'edit' => array(),
                 'delete' => array(),
             )
         ))
      ;
   }

   protected function configureRoutes(RouteCollection $collection)
    {
      // remove the "Add New" button
      //$collection->remove('create');

    }

   public function prePersist($image) {
      $this->manageFileUpload($image);
   }

   public function preUpdate($image) {
      $this->manageFileUpload($image);
   }

   private function manageFileUpload($image) {
      if ($image->getFile()) {
         $image->refreshUpdated();
      }
   }

}

0 个答案:

没有答案