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.

<?php
	// Remember to copy files from the SDK's src/ directory to a
	// directory in your application on the server, such as src/
	require_once('src/facebook.php');

	$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>
<html>
<head>
<meta charset="utf-8">
<title>Facebook Upload Photo to User's Profile</title>
</head>
<body>

<a href='http://1stwebmagazine.com/facebook-post-to-wall'><img src='../img/logo.png' alt='1stWebMagazine' /></a>
<h1>Facebook Post to Wall</h1>


<?php
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>';
		error_log($e->getType());
		error_log($e->getMessage());
	}   
} 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>';
}
?>
  
</body>
</html>

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 »


Go Daddy


Related articles

Email Validation

Email Validation

08/02/2013

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

04/02/2013

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

29/01/2013

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

Tags

Get online with the world's Best - GoDaddy.com!

Back to Top