79804681

Date: 2025-10-30 11:09:14
Score: 1
Natty:
Report link

Update based on @h3llr4iser answer:

<?php

namespace App\Services;

use App\Entity\MyEntity;
use Vich\UploaderBundle\FileAbstraction\ReplacingFile;
use Vich\UploaderBundle\Handler\UploadHandler;

final class ExternalImageUploader
{
    public function __construct(private UploadHandler $uploadHandler)
    {
    }

    public function copyExternalFile(string $url, MyEntity $entity)
    {
        $newfile = tempnam(sys_get_temp_dir(), 'prefix_');
        copy($url, $newfile);
        $uploadedFile = new ReplacingFile($newfile);

        $entity->setImageFile($uploadedFile);
        $this->uploadHandler->upload($entity,'imageFile');
    }
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @h3llr4iser
  • Low reputation (1):
Posted by: moux2003