draw62188 2016-11-06 21:53
浏览 76
已采纳

通过URL输入设置Symfony上传文件

Via an XML import I'm getting also an url link to jpg images on an external server. I want to copy the images. I'm tried to do this via UploadedFile() function in Symfony to then add them in my entity.

Via the following code I want to add the url image in the Symfony image object.

$f = new UploadedFile();
$f->openFile($imageUrl);

So after opening the file I want to add them in my image entity.

$image = new image();
$image->setFile($f);

This entity contains the following file parameters:

/**
* 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;
}

Then I get the error that arguments are missing. But with an from it works with the following code:

$image = new image();
$image->setFile($request->files->get('images'));
  • 写回答

2条回答 默认 最新

  • doucong1853 2016-11-07 09:21
    关注

    To download a file from a url with php you will need to take a look at : http://php.net/manual/fr/function.file-put-contents.php

    With Symfony File object, To persist an Image if you have an image entity :

    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;
        }
    }
    

    if you want to specify some validation rules using annotations

    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;
    }
    

    Then the upload function that move the 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;
    }
    

    And then in controller do something like this

    $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();
    

    This is a quick example to help you, might not work with copy past (and it's not the purpose).

    Take a look at : http://symfony.com/doc/current/controller/upload_file.html

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?