Create photo albums and upload photos using the Facebook Graph API

By David Pratt / Tags: , / 71 Comments / Published: 11-09-10

UPDATE: Thanks to some of the comments below I have been able to revise the code – thanks Luke & Guilherme

I thought I’d share some recent learning’s with using the Facebook Graph API as there seems to be so few examples out there about how to utilise various aspects of its functionality. I will explain how to create a photo album and then insert a photo into it using PHP.

The code example assumes that you have already generated an authenticated session and have the correct permissions (read_stream, publish_stream, photo_upload, user_photos, user_photo_video_tags). If you haven’t already done so, then there is a good example here about how to do so and you can read more about the Facebook permissions on the Facebook dev site.

The following snippet creates a photo album and then uploads a photo into it:

//At the time of writing it is necessary to enable upload support in the Facebook SDK, you do this with the line:
$facebook->setFileUploadSupport(true);
 
//Create an album
$album_details = array(
		'message'=> 'Album desc',
		'name'=> 'Album name'
);
$create_album = $facebook->api('/me/albums', 'post', $album_details);
 
//Get album ID of the album you've just created
$album_uid = $create_album['id'];
 
//Upload a photo to album of ID...
$photo_details = array(
	'message'=> 'Photo message'
);
$file='app.jpg'; //Example image file
$photo_details['image'] = '@' . realpath($file);
 
$upload_photo = $facebook->api('/'.$album_uid.'/photos', 'post', $photo_details);

You can add more detail to the created album and photo than I have in the example by using additional parameters within the arguments array.

Category: Tech

Tags: ,

Posted: on September 11th, 2010 at 6:08 pm.

Feeds: RSS 2.0

71 Responses to “Create photo albums and upload photos using the Facebook Graph API”

Lee October 14th, 2011 at 9:01 pm

Hi, thanks for your help. I think this code doesn’t work now though with the updated Facebook API. Can you please update this so it becomes compatible with the new API? Thanks!

Phuc October 22nd, 2011 at 12:10 pm

The code work fine with facebook profile and profile album but I am on way to try to upload to my facebook fan page, any idea for this? I used album id from fan page for album id but it doesn’t help.

Hardrock November 1st, 2011 at 10:23 am

pls upadte the code

Arvind Bhardwaj November 6th, 2011 at 9:31 am

Hi,
Its a nice article.
I have also written an article for uploading photos on Facebook photo album. You can try it here:http://www.webspeaks.in/2011/08/upload-photos-on-facebook-using-php.html

Ritika November 7th, 2011 at 12:11 pm

hy please send me copmlete tutorial on how to upload pic on facebook

Karan Adep November 24th, 2011 at 9:46 am

Great and simplified tutorial , thanks a ton

Gandhar January 16th, 2012 at 8:19 am

how can i create an album in a group?

I get the following error when I try to create an album in a group
(#10) Application does not have permission for this action

Following is my code:-
$facebook = new Facebook(array(
‘appId’ => APP_ID,
‘secret’ => SECRET_ID,
‘cookie’ => true
));
$loginUrl = $facebook->getLoginUrl( array(‘scope’ => ‘read_stream,publish_stream,photo_upload,user_photos, user_photo_video_tags,user_groups,offline_access’));
$album_details = array(
‘message’=> ‘Test Album from api’,
‘name’=> ‘Test Album’
);
$create_album = $facebook->api(GROUP_ID.’/albums’, ‘post’, $album_details);

Angelo January 21st, 2012 at 11:14 pm

So helpfull, thanks a lot guy !

Keith Lantz January 31st, 2012 at 6:53 pm

Any updates on Gandhar’s issue? We receive the same error when attempting to create an album within a group.

Adiraz February 6th, 2012 at 9:33 am

hey friends..i can creating an album though php code.but i can’t upload a image in album. anybody here who can help me.

so please i request his/her contact me on fb.

http://facebook.com/adiraz786
http://facebook.com/adirazrocks

Thanks In advanced

Sam March 30th, 2012 at 10:44 pm

+1 on Gandhar’s issue, this just started happening for no reason in my application.

ignacio April 15th, 2012 at 11:00 pm

i am sorry for the foolish question, but when I must place this .php code? And from where is it called?

naveed khan April 17th, 2012 at 3:16 pm

Thanks its work fine, thanks a lot for your great support..

Sky July 30th, 2012 at 12:18 pm

Hey guys. Need help urgently. For this app function of photo uploading, should i set query string or URI Fragment for the Auth Token Parameter?

nazir September 1st, 2012 at 6:42 am

thanks a lot, you save my life, thank you

ashish September 5th, 2012 at 6:31 pm

Hi, Nice tutorial. Just out of curosity, Can we upload photos/videos in past date ?

arth September 27th, 2012 at 8:58 am

if i want to upload photo to page created by me then..??

please give me right solution

Sébastien November 14th, 2012 at 2:50 am

Why i got this error ??

Uncaught OAuthException: (#100) Invalid ID for album owner thrown

jay December 31st, 2012 at 11:50 am

Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in D:\wamp\www\fb\facebook\examples\base_facebook.php on line 1239

Robert January 6th, 2013 at 3:50 pm

Hi, I’m getting this error:

Fatal error: Uncaught CurlException: 26: failed creating formpost data thrown in base_facebook.php on line 972

Can anyone help please?

Thanks,

Robert

DonDinh May 6th, 2014 at 3:56 am

Hello, Any updates on Gandhar’s issue? I got the same issue.
Please help me
Thanks a lot

Leave a reply