Tuesday, January 18, 2022

[FIXED] Set Symfony uploaded file by URL input

Issue

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'));

Solution

To download a file from a url with php you will need to take a look at : http://php.net/manual/fr/function.file-get-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



Answered By - Thomas C

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.