config:
...
mappings:
product_image:
uri_prefix: /images/products
upload_destination: %kernel.root_dir%/../web/images/products
Entity:
/**
* @Vich\UploadableField(mapping="product_image", fileNameProperty="productImage")
*/
protected $file;
I try this:
use Symfony\Component\HttpFoundation\File\File;
....
$path = '/tmp/someImage.jpg';
$imageFile = new File($path);
$entity->setFile($imageFile);
but it does not work :(
Any ideas?
You should put 'someImage.jpg' in your filename property which is the one that will be persisted eventually.
ok, but it does not upload file to destination directory which set in "upload_destination"
Have I upload this file manually?
You can try to set an UploadedFile instance instead of a File. To construct an UploadedFile you'll need to define some extra information (see the constructor of UploadedFile for details).
The only alternative would be to put the file directly into the upload destination.
Hey, simple exemple ;)
<?php
namespace AppBundle\DataFixtures\ORM;
use AppBundle\Entity\Document;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class LoadDocumentsData extends AbstractFixture implements OrderedFixtureInterface
{
public function load(ObjectManager $manager) {
$src = __DIR__."/../Resources/doc/mydocument.pdf";
$file = new UploadedFile(
$src,
'mydocument.pdf',
'application/pdf',
filesize($src),
null,
true // Set test mode true !!! " Local files are used in test mode hence the code should not enforce HTTP uploads."
);
$document = new Document();
$document->setDocumentFile($file);
$manager->persist($document);
$manager->flush();
}
}
Most helpful comment
Hey, simple exemple ;)