[英]Set Symfony uploaded file by URL input
通过 XML 导入,我还获得了一个指向外部服务器上 jpg 图像的 url 链接。 我想复制图像。 我尝试通过 Symfony 中的 UploadedFile() 函数执行此操作,然后将它们添加到我的实体中。
通过以下代码,我想在 Symfony 图像对象中添加 url 图像。
$f = new UploadedFile();
$f->openFile($imageUrl);
所以打开文件后,我想将它们添加到我的图像实体中。
$image = new image();
$image->setFile($f);
该实体包含以下文件参数:
/**
* Sets file.
*
* @param \Symfony\Component\HttpFoundation\File\UploadedFile $file
*/
public function setFile(UploadedFile $file = NULL) {
$this->file = $file;
}
/**
* Get file.
*
* @return UploadedFile
*/
public function getFile() {
return $this->file;
}
然后我得到参数丢失的错误。 但是使用 from 它可以使用以下代码:
$image = new image();
$image->setFile($request->files->get('images'));
我使用 vich uploader bundle ( https://github.com/dustin10/VichUploaderBundle ) 上传图片,bundle 提供了一种从表单上传图片的方法。 我有和你一样的用例。 我必须从 xml 导入图像。
我使用 vich 服务 (vich_uploader.upload_handler) 从 xml 中做到这一点:
实体:
namespace AppBundle;
use Doctrine\ORM\Mapping AS ORM;
use Symfony\Component\HttpFoundation\File\File;
/**
* @ORM\Table(name="entity")
* @Vich\Uploadable
*/
class Entity
{
/**
* @ORM\Column(type="string", nullable=true)
*/
private $image;
/**
* @Vich\UploadableField(mapping="test_image", fileNameProperty="image")
* @var File
*/
private $imageFile;
/**
* @return File
*/
public function getImageFile()
{
return $this->imageFile;
}
/**
* @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
*/
public function setImageFile(File $image = null)
{
$this->imageFile = $image;
if ($image) {
// It is required that at least one field changes if you are using doctrine
// otherwise the event listeners won't be called and the file is lost
$this->updatedAt = new \DateTime('now');
}
}
}
服务:
<?php
namespace AppBundle;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Vich\UploaderBundle\Handler\UploadHandler;
class ExternalImageUploader
{
private $uploadHandler;
private $kernelRootDir;
public function __construct(UploadHandler $uploadHandler, $kernelRootDir)
{
$this->uploadHandler = $uploadHandler;
$this->kernelRootDir = $kernelRootDir;
}
public function copyExternalFile($url, $entity)
{
$newfile = $this->kernelRootDir.'/../web/uploads/test.jpg';
copy($url, $newfile);
$mimeType = mime_content_type($newfile);
$size = filesize ($newfile);
$finalName = md5(uniqid(rand(), true)).".jpg";
$uploadedFile = new UploadedFile($newfile,$finalName,$mimeType, $size);
$entity->setImageFile($uploadedFile);
$this->uploadHandler->upload($entity,'imageFile');
}
}
服务.xml
<service id="app.external_image_uploader" class="AppBundle\ExternalImageUploader">
<argument type="service" id="vich_uploader.upload_handler" />
<argument>%kernel.root_dir%</argument>
</service>
要使用 php 从 url 下载文件,您需要查看: http : //php.net/manual/fr/function.file-get-contents.php
使用 Symfony File 对象,如果你有一个图像实体,要保留一个图像:
namespace AppBundle\Entity;
use Symfony\Component\HttpFoundation\File\File;
class Image
{
protected $file;
public function setFile(File $myFile = null)
{
$this->file = $myFile;
}
public function getFile()
{
return $this->file;
}
}
如果你想使用注解指定一些验证规则
use Symfony\Component\Validator\Constraints as Assert;
class Image
{
/**
* @Assert\File(
* maxSize = "1024k",
* mimeTypes = {"image/jpeg", "image/png"},
* mimeTypesMessage = "Please upload a valid image"
* )
*/
protected file;
}
然后是移动文件的上传功能
public function upload(File $file)
{
//generate unique filename
$fileName = md5(uniqid()).'.'.$file->guessExtension();
//Set other entity attribute here
//move the file
$file->move($targetDirectory, $fileName);
return $fileName;
}
然后在控制器中做这样的事情
$em = $this->getDoctrine()->getManager();
$image = new Image();
$file = file_get_contents('http://www.example.com/');
$image->setFile($file);
$image->upload();
//Maybe persist your entity if you need it
$em->persist($image);
$em->flush();
这是一个可以帮助您的快速示例,可能不适用于复制过去(这不是目的)。
看看: http : //symfony.com/doc/current/controller/upload_file.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.