通过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'));
答案 0 :(得分:3)
我使用vich uploader bundle(https://github.com/dustin10/VichUploaderBundle)上传图片,该套装提供了一种从表单上传图片的方法。我有与你相同的用例。我必须从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');
}
}
的services.xml
<service id="app.external_image_uploader" class="AppBundle\ExternalImageUploader">
<argument type="service" id="vich_uploader.upload_handler" />
<argument>%kernel.root_dir%</argument>
</service>
答案 1 :(得分:0)
要使用php从网址下载文件,您需要查看:http://php.net/manual/fr/function.file-put-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