I’m trying to use the facebook api to get the app user’s profile pic and then save it on the server. I’m getting the image just fine, but creating a new image file and saving it in the correct folder seems to be a problem. I tried using the fopen and file_put_contents functions, but apparently they require that a file be created before hand. How can I save the fb user’s image on the server? My code is as follows.
$facebook = new Facebook(array(
'appId' => '12345',
'secret' => '12345',
'cookie' => true
));
$access_token = $facebook->getAccessToken();
if($access_token != "")
{
$user = $facebook->getUser();
if($user != 0)
{
$user_profile = $facebook->api('/me');
$fb_id = $user_profile['id'];
$fb_first_name = $user_profile['first_name'];
$fb_last_name = $user_profile['last_name'];
$fb_email = $user_profile['email'];
$img = file_get_contents('https://graph.facebook.com/'.$fb_id.'/picture?type=large');
$file = '/img/profile_pics/large/';
rename($img, "".$file."".$img."");
}
Any suggestions?
Thanks,
Lance
Using
$img = file_get_contents(...)img will contain the source of the image just save that to a file,rename()wont work.Just do: