Okay, So this one is really simple. I was working with an api which offered me user profile with profile image url which i needed to store on my machine. The task was quite simple and if I was working with Java I would be using streams etc, But its php and you get so many things inbuild, all you need to know is if it exists. If not then huge repositories from composer will help you out.
Back to the story. So here’s how I achieved the task.
/*$profile is an array response from api service containing url of the profile picture*/ $imagepath = $this->getParameter('kernel.root_dir').'/media/image'; $profilePicture = file_get_contents($profile['pictureUrl'] ); file_put_contents($this->imagepath . '/avatars/avatar/somefilename.png', $profilePicture);
And that’s it, file_get_contents
take the stream of data and we store it on filesystem using file_put_contents
.
As I mentioned in title I was using symfony so I was able to get root directory and image path using controllers getParameter
method.
Simple Right? Sharing this information because sometimes it takes lot of googling to find such simple thing. Hope it helps a fellow coder.
Happy Coding!