1

The upload of my images with the Vich Uploader Bundle is working fine, they are stored in the correct directory and in the database. However when trying to access them or display them, they are not find. (Object not found exception or just displaying an empty square.

Any ideas why this might be?

the config:

vich_uploader:
    db_driver: orm
    mappings:
       carousel:
            uri_prefix: %kernel.root_dir%/web/uploads/carousel
            upload_destination: '%kernel.root_dir%/web/uploads/carousel'
            namer: vich_uploader.namer_origname

Also, my setter of the imageFile is not working at all. When uploading an Image for the first time, everything is set (updatedAt, imageSize, imageName) except for the imageFile, which is always null..

e.g.

  Image {#1601 ▼ 
    -id: 1 
    -imageFile: null 
    -imageSize: 34703 
    -imageName: "5a7f9db650f78_EntityType.png" 
    -updatedAt: DateTime {#1599 ▼ 
    } 
  }

here is my Entity:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* Image
*
* @ORM\Table(name="app_image")
* @ORM\Entity
* @Vich\Uploadable
*/
class Image {

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

  /**
   * NOTE: This is not a mapped field of entity metadata, just a simple property.
   *
   * @Vich\UploadableField(mapping="carousel", fileNameProperty="imageName", size="imageSize")
   * @Assert\File()
   * @var File
   */
  private $imageFile;

  /**
    * @ORM\Column(type="integer")
    *
    * @var integer
    */
   private $imageSize;

   /**
    * @var string
    * @ORM\Column(type="string", nullable=false)
    */
   private $imageName;

   /**
   * @ORM\Column(type="datetime")
   *
   * @var \DateTime
   */
  private $updatedAt;

   public function getImageFile(){
     return $this->imageFile;
   }

    /**
    *
    * @param \Symfony\Component\HttpFoundation\File\File|\Symfony\Component\HttpFoundation\File\UploadedFile $file
    *
    * @return File
  */
    public function setImageFile(\Symfony\Component\HttpFoundation\File\File $image = null)
    {
      $this->imageFile = $image;
      if ($image instanceof File) {
        $this->updatedAt = new \DateTime();
      }
      return $this;
    }


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

    /**
     * Set updatedAt
     *
     * @param \DateTime $updatedAt
     *
     * @return Image
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;

        return $this;
    }

    /**
     * Get updatedAt
     *
     * @return \DateTime
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }

    /**
     * Set imageSize
     *
     * @param integer $imageSize
     *
     * @return Image
     */
    public function setImageSize($imageSize)
    {
        $this->imageSize = $imageSize;

        return $this;
    }

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

    /**
     * Set imageName
     *
     * @param string $imageName
     *
     * @return Image
     */
    public function setImageName($imageName)
    {
        $this->imageName = $imageName;

        return $this;
    }

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

}

I have the feeling that this is caused by a 'deeper' problem in my installation or anything, because everything is working fine until that point. If I e.g. the part with

      if ($image instanceof File) {
    $this->updatedAt = new \DateTime();
  }

of the setter is commented out, I get an updatedAt can't be null exception, so somehow the setter is accessed but it's not setting the image as an Uploaded File.

Am I using the wrong directory or wrong access rights, or what else could it be?

halfer
  • 19,824
  • 17
  • 99
  • 186
sonja
  • 924
  • 1
  • 21
  • 52
  • `upload_destination: '%kernel.root_dir%/web/uploads/carousel'` Should'nt be it : `upload_destination: '%kernel.root_dir%/../web/uploads/carousel'` ? – henrily Feb 13 '18 at 16:16
  • @henrily I just changed this, minutes ago but it didn't change anything, I really feel like the uri_prefix is not working concerning that issu – sonja Feb 13 '18 at 16:32
  • Yes the uri_prefix should be your `/path/to/image` also If your image is host in the `web/uploads/carousel` I guess the uri_prefix should be `/uploads/carousel/` (not sure about the /) – henrily Feb 13 '18 at 16:35
  • @henrily I added /uploads/carousel but it still says object not found when then adding /sp/web/ (sp is the project name) the image is found. I don't really want to hard code /sp/web in my config.yml file. maybe there is a way of adding that in the twig file? – sonja Feb 13 '18 at 16:43
  • You don't need to hardcoded this you can add this into the parameters.yml (and also in parameters.yml.dist) and use it in the config.yml: `uri_prefix: '%carousel_uri_prefix%'` and in parameters.yml: `carousel_uri_prefix: '/your/path/to/image'` – henrily Feb 13 '18 at 16:49
  • @henrily and what is my/path/to/image? sorry :D – sonja Feb 13 '18 at 16:55
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/165072/discussion-between-henrily-and-sonja). – henrily Feb 13 '18 at 16:56
  • is it one server? or may be something like a cluster with load balancing? – Murat Erkenov Feb 14 '18 at 07:08
  • @MuratErkenov thank you, but henrily already helped me out :) – sonja Feb 14 '18 at 12:51

3 Answers3

0

I had the same issue. At least on my side, it is a server configuration thing.

If you run syfmony's server, it will work like a charm php bin/console server:run

If you run php server, images won't be displayed php -S 127.0.0.1:8000 -t public

0

public function configureFields(string $pageName): iterable { return [

        TextField::new('name'),
        //TextareaField::new('imageFile')->setFormType(VichImageType::class)->hideOnIndex(),
        AssociationField::new('category'),
        BooleanField::new('status'),
        NumberField::new('price'),
        //ImageField::new('imageFile')->setBasePath($this->getParameter("app.path.product_images"))->onlyOnIndex(),

ImageField::new('imageFile')->setFormType(VichImageType::class)->onlyOnIndex(),

     ImageField::new('image',"product image")->setBasePath('/uploads/images/products/')->setUploadDir('/public/uploads/images/products/')->setUploadedFileNamePattern('[randomhash],[extension]')->setRequired(false),
     TextField::new('description'),
    ];
}
0

vich_uploader: db_driver: orm

mappings:
    product:
        uri_prefix: '%app.path.product_images%'
        upload_destination: '%kernel.project_dir%/uploads/images/products'
        namer: Vich\UploaderBundle\Naming\SmartUniqueNamer 
        inject_on_load:     false
        delete_on_remove : false
        delete_on_update: false