Facebook Upload Photo to User′s Profile

Learn how to upload a photo to Facebook user's profile!  First you need to create a new app on Facebook. If you do not now how to do it, read this.

Facebook Upload Photo to User's Profile script

This script upload a photo to user's profile via PHP SDK.
Replace YOUR_APP_ID and YOUR_APP_SECRET to your App ID and App Secret  in the following code and save it as index.php.

	// Remember to copy files from the SDK's src/ directory to a
	// directory in your application on the server, such as src/

	$config = array(
		'appId' => 'YOUR_APP_ID',
		'secret' => 'YOUR_APP_SECRET',
		'fileUpload' => true,

	$facebook = new Facebook($config);
	$user_id = $facebook->getUser();
	$photo = '/img/facebook-upload-picture.jpg'; // Path to the photo on the local filesystem
	$message = 'Photo upload via the PHP SDK!';

<!doctype html>
<meta charset="utf-8">
<title>Facebook Upload Photo to User's Profile</title>

<a href=''><img src='../img/logo.png' alt='1stWebMagazine' /></a>
<h1>Facebook Post to Wall</h1>

if($user_id) {

	// We have a user ID, so probably a logged in user.
	// If not, we'll get an exception, which we handle below.
	try {

		// Upload to a user's profile. The photo will be in the
		// first album in the profile. You can also upload to
		// a specific album by using /ALBUM_ID as the path 
		$ret_obj = $facebook->api('/me/photos', 'POST', array(
									 'source' => '@' . $photo,
									 'message' => $message,
		echo '<pre>Photo ID: ' . $ret_obj['id'] . '</pre>';
		echo '<br /><a href="' . $facebook->getLogoutUrl() . '">logout from Facebook</a>';
	} catch(FacebookApiException $e) {
		// If the user is logged out, you can have a 
		// user ID even though the access token is invalid.
		// In this case, we'll get an exception, so we'll
		// just ask the user to login again here.
		$login_url = $facebook->getLoginUrl( array(
					   'scope' => 'photo_upload'
		echo 'Please <a href="' . $login_url . '">login.</a>';
} else {

	// No user, print a link for the user to login
	// To upload a photo to a user's wall, we need photo_upload  permission
	// We'll use the current URL as the redirect_uri, so we don't
	// need to specify it here.
	$login_url = $facebook->getLoginUrl( array( 'scope' => 'photo_upload') );
	echo 'Please <a href="' . $login_url . '">login.</a>';

Copy index.php and the src directory from the PHP SDK to the that folder on your website what you entered earlier for your app.


Demo »


Related articles

Email Validation

Email Validation


Learn how to validate email on client side with HTML5, Javascript and on server side with PHP.

Generate Thumbnail on the Fly with PHP

Generate Thumbnail on the Fly with PHP


In a previous artice I already showed you how to generate square thumbnails with PHP. Now I show you how to keep the original ratio of the image and how to control the alignment of the square image.

Facebook Post to Wall

Facebook Post to Wall


This tutorial shows you how to post to wall with PHP SDK.

Leave a comment

We welcome any comment from you! Please keep in mind that comments are moderated and rel='nofollow' is in use. So, please do not use a domain as your name or a spammy keyword, or your comment will be deleted.

Name: *

E-mail: *

Message: *

Click on the baby *

Find us on Facebook



Back to Top