设置Symfony上传文件的URL输入


Set Symfony uploaded file by URL input

通过XML导入,我也得到一个url链接到外部服务器上的jpg图像。我想复制图像。我试图通过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'));

我使用哪个上传器bundle (https://github.com/dustin10/VichUploaderBundle)来上传图像,这个bundle提供了一种从表单上传图像的方法。我和你有同样的用例。我必须从xml文件中导入图像。

我使用哪个服务(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 .

<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