Introduction: This article is compiled by the editor of Programming Notes# for you. It mainly introduces the knowledge related to saving images from URLs without forms (Symfony 3). I hope it has certain reference value for you.
I want to save an image (from URL) and relocate it to another folder without creating a form to the controller. How can I realize this?
Download image in Controller -> relocate it to another folder -> save image name into my database (in existing table).
Answer
Your question is broad, you didn’t specify which Doctrine DBAL and ORM you’re using and what exactly your question is, so I’m assuming you do use them and you know how to control Inject the entity manager in the manager action.
First you have to download the image and save the image:
$cOntent= file_get_contents("http://example.com/image.jpg");
//Store in the filesystem.
$fp = fopen("/location/to/save/image.jpg", "w");
fwrite($fp, $content);
fclose( $fp);
Then save the path to the database:
$imageEntity->setPath('image.jpg');
$entityManager->flush($imageEntity);