NoC*_*sum 5 php symfony vichuploaderbundle
一个基本的 Symfony 3 应用程序安装并配置了VichUploader 包,并带有一个用于上传文件的实体。
如何在我的 ORM 数据装置中将文件附加到该实体?
在 Symfony 2 中有一个关于这个问题的问题,我已经尝试过该代码。夹具加载没有错误,但“上传”的文件不会复制到其最终目的地。
我可以手动执行此操作,我最近的尝试如下所示:
<?php
// src/AppBundle/DataFixtures/ORM/LoadCourseData.php
namespace AppBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\Persistence\ObjectManager;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\File\MimeType\MimeTypeGuesser;
use AppBundle\Entity\Course;
class LoadCourseData extends AbstractFixture implements OrderedFixtureInterface
{
public function load(ObjectManager $manager)
{
$course = new Course();
$course->setName('How to make data fixtures in Symfony lol')
->setAuthor($this->getReference('admin-user'))
->setSummary('You\'ll know when I know!')
->setLicense($this->getReference('cc-by'))
->setDifficulty(1);
// Persist to generate slug (to be used for uploads)
$manager->persist($course);
// Attach file
// I copy it to a temporary directory as per the Symfony2 answer
copy(__DIR__.'/../Files/kitten.jpg', '/tmp/kitten.jpg');
// These are filled in for completeness
$mimetype = MimeTypeGuesser::getInstance()->guess('/tmp/kitten.jpg');
$size = filesize('/tmp/kitten.jpg');
// Create a new UploadedFile
$file = new UploadedFile('/tmp/kitten.jpg', 'kitten.jpg', $mimetype, $size, null, true);
// I have to move it manually?
// Plus how can I take advantage of the VichUploader namer?
$file->move(__DIR__.'/../../../../web/img/upload/course/');
// Apply to entity
$course->setImageFile($file);
// Persist
$manager->persist($course);
$manager->flush();
}
public function getOrder()
{
return 4;
}
}
Run Code Online (Sandbox Code Playgroud)
我的解决方案仍然需要大量工作!我是不是叫错了树?
config.yml. 我应该从那里访问它们或将它们转换为参数我发现 GitHub 上的 VichUploader 文档不太好,所以我建议你使用 Symfony 文档: http://symfony.com/doc/current/controller/upload_file.html
我在 src/AppBundle 下创建了一个 FileUploader.php,如下所示:
<?php
// src/AppBundle/FileUploader.php
namespace AppBundle;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class FileUploader
{
private $targetDir;
public function __construct($targetDir)
{
$this->targetDir = $targetDir;
}
public function upload(UploadedFile $file, $path, $name)
{
$fileName = $name.'.'.$file->guessExtension();
$file->move($this->targetDir.$path, $fileName);
return $fileName;
}
}
Run Code Online (Sandbox Code Playgroud)
这允许您指定文件、路径和名称。
然后在您的 app/config/service.yml 文件中添加以下内容以引用它:
services:
app.attachment_uploader:
class: AppBundle\FileUploader
arguments: ['%document_directory%']
Run Code Online (Sandbox Code Playgroud)
然后您需要在 app/config/parameters.yml 文件中指定 %document_directory%:
parameters:
...
document_directory: documents
Run Code Online (Sandbox Code Playgroud)
就我而言,上述“文档”文件夹位于 web 文件夹下,因此路径为“web/documents”。我想将所有内容存储在同一个位置。
然后我在任何控制器中使用 FileUploader,如下所示:
...
->add('catalog', FileType::class, array( // Docs
'label' => 'Catalog Course Description ',
'required' => false,
'attr' => array(
'accept' => 'image/*,application/pdf,text/plain,application/msword',
'onchange' => "checkFileSize('form_catalog')"
),
))
...
$file = $form->get("catalog")->getData();
$fileName = $this->get('app.attachment_uploader')->upload( $file,
'/'.$pet->getStudent()->getBanId(),
$pet->getCourse()->getCorTitle().'_'.'Catalog_Course_Desc'
...
$att_cat->setDocument($fileName);
$em->persist($att_cat);
$em->flush();
Run Code Online (Sandbox Code Playgroud)
在上面的代码中,我获取了“目录”文件按钮数据,即文件。然后调用指定文件、路径和文件名参数的“上传”方法。然后保留该实体 ($att_cat)。
上面的使用方法非常简单。
| 归档时间: |
|
| 查看次数: |
1383 次 |
| 最近记录: |