Uploading a photo to the user’s profile is an easy task using the Facebook Graph API.

As mentioned in the Facebook blog post:

In order to publish a photo to a user’s album, you must have the publish_stream permission. With that granted, you can upload a photo by issuing an HTTP POST request with the photo content and an optional description to one these to Graph API connections:

  • https://graph.facebook.com/USER_ID/photos – The photo will be published to an album created for your app. We automatically create an album for your app if it does not already exist. All photos uploaded this way will then be added to this same album.
  • https://graph.facebook.com/ALBUM_ID/photos – The photo will be published to a specific, existing photo album, represented by the ALBUM_ID.

So basically, you need the publish_stream permission and using one of the following approaches:

Without using the PHP-SDK:

error_reporting(E_ALL & ~E_NOTICE);
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
$my_url = "REDIRECT_URI"; // mainly, redirect to this page
$perms_str = "publish_stream";

$code = $_REQUEST["code"];

if(empty($code)) {
	$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
	. $app_id . "&redirect_uri=" . urlencode($my_url)
	. "&scope=" . $perms_str;
	echo("<script>top.location.href='" . $auth_url . "'</script>");

$token_url = "https://graph.facebook.com/oauth/access_token?client_id="
. $app_id . "&redirect_uri=" . urlencode($my_url)
. "&client_secret=" . $app_secret
. "&code=" . $code;
$response = file_get_contents($token_url);
$p = null;
parse_str($response, $p);
$access_token = $p['access_token'];
$graph_url= "https://graph.facebook.com/me/photos?"
         . "access_token=" .$access_token;
if (!empty($_FILES)) {
	$params = array();
	if( isset($_POST['message']) ) {
		$params['message'] = trim($_POST['message']);
	$uploaddir = './uploads/'; // Upload folder
	$uploadfile = $uploaddir . basename($_FILES['source']['name']);
	if (move_uploaded_file($_FILES['source']['tmp_name'], $uploadfile)) {
		$params['source'] = "@" . realpath($uploadfile);
	// Start the Graph API call
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL,$graph_url);
		Next option is only used for 
		user from a local (WAMP) 
		machine. This should be removed
		when used on a live server!
	//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    $result = curl_exec($ch);
    $decoded = json_decode($result, true);
	if(is_array($decoded) && isset($decoded['id'])) {
			Picture is uploaded successfully:
			1) show success message
			2) optionally, delete image from our server
		$msg = "Image uploaded successfully: {$decoded['id']}";
<!doctype html>
label {float: left; width: 100px;}
input[type=text],textarea {width: 210px;}
#msg {border: 1px solid #000; padding: 5px; color: red;}
<?php if( isset($msg) ) { ?>
<p id="msg"><?php echo $msg; ?></p>
<?php } ?>
<form enctype="multipart/form-data" action="" method="post">
	<p><label for="name">Caption</label><input type="text" name="message" value="" /></p>
	<p><label for="source">Photo</label><input type="file" name="source" /></p>
	<p><input type="submit" value="Upload" /></p>

Photo UploadSuccessful Photo Upload

Please note that file upload snippet is for demonstration purpose only, you should NOT use it on live websites!

Using the Facebook PHP-SDK

$file = FCPATH . "assets/img/small1.jpg";
$post_data = array(
    "message" => "My photo caption",
    "source" => '@' . realpath($file)
$data['photo'] = $this->facebook->api("/me/photos", 'post', $post_data);  

Code was taken from my answer here.

Back to top
  • http://www.facebook.com/eliwedel Eli Wedel

    Is it possible to upload a photo to facebook that is already online. For example.. I have a photo gallery of people and I’d like them to be able to upload those photos directly from my site to facebook.  Is that doable or do they have to download it to their computer first?

    • http://www.facebook.com/profile.php?id=654770068 Muhammadh Amin

       u can upload from ur site, i am actually doing that, in my case my project is for a photographer he wanted to upload selected some photos to his FB from the gallery he have at his site, and ask the facebook users to go to his site for all the photos,

      • http://www.facebook.com/KelliDandelake Kelli Dandelake

        how are you doing that?

      • Mani

        i’m also need to upload photos from my site to facebook without saving them to my computer. i need your assitance. thanks

  • http://www.facebook.com/profile.php?id=654770068 Muhammadh Amin

    thanks man for the replay although i have figured it out ready, Thanks again, 😀

  • Mchprod

    Hello. Thanks so much for this script.
    I got a question… with this script, the user gets redirected to the $my_url adress, instead of being in facebook and uses de the app withing an iframe.
    And, how can I correct this?
    Thanks in advice.

  • Mozack


    I was checking this code (awesome) and it works as expected. But i have one question. I’m trying to find a solution to:

    User upload from my website to my facebook fan page photo album (defined by me) instead of upload to users album.

    Is there any way to do that? I want users upload their photos to my fan page to make a contest with app.

    Maybe the big resource to that is, user post to my album and also post in their wall telling friends that they are in contest, with a link to their photo.

    How can i do that?



  • brijkishor rajput

    Can we set uploaded PIC as a user main profile PIC

  • http://www.facebook.com/profile.php?id=708266359 Daniel Ireson

    Hi. Great tutorial, just like all your other ones on using the Facebook API. Keep up the good work. Thanks!

  • Shehzad Mo

    how to read image properties like (url, likes) of facebook image id i get after uploading…?

  • http://www.facebook.com/gianluca.suarato Gianluca Suarato

    can i upload into fan page ?

  • http://www.facebook.com/nikz99 Nikz Kamal

    Hi Materingapi.
    Can i have please  this photo upload to friend wall script for asp.net C# site ?
    or anyone else cane help me

  • Joby Joseph

    Learn how to Upload Photos to Facebook Using PHP SDK, 


    Live demo and source code available

  • Vanessah2478

    hi! do you have the same code but in javascript? 

  • http://www.facebook.com/breitlingmusicgroup Tee Mo MuLa


  • Jay Bhojwani
  • Jay Bhojwani

    reply me soon

  • Jay Bhojwani

    i am using this code also its giving an parser error

    ‘secret’ => ‘bec**************b0e607ae6e2’,
    ‘fileUpload’ => true,

    // Get User ID
    $user = $facebook->getUser();

    // We may or may not have this data based on whether the user is logged in.
    // If we have a $user id here, it means we know the user is logged into
    // Facebook, but we don’t know if the access token is valid. An access
    // token is invalid if the user logged out of Facebook.

    if ($user) {
    try {
    // Proceed knowing you have a logged in user who’s authenticated.
    $user_profile = $facebook->api(‘/me’);
    } catch (FacebookApiException $e) {
    $user = null;
    $params = array(
    ‘scope’ => ’email,user_hometown,user_birthday,user_interests,user_likes,user_location’,
    // Login or logout url will be needed depending on current user state.
    if ($user) {
    $logoutUrl = $facebook->getLogoutUrl();

    } else {
    $loginUrl = $facebook->getLoginUrl($params);


    $img =”example.jpg”‘;

    $photo = $facebook->api(`/me/photos`, `POST`,array(`source` => `@` . $img,`message` => `Photo uploaded via the PHP SDK`));



    body {
    font-family: ‘Lucida Grande’, Verdana, Arial, sans-serif;
    h1 a {
    text-decoration: none;
    color: #3b5998;
    h1 a:hover {
    text-decoration: underline;


    <a href="”>Logout

    Login using OAuth 2.0 handled by the PHP SDK: <a href="”>Login with Facebook

    PHP Session

    <img src="https://graph.facebook.com//picture“>
    Your User Object (/me)

    You are not Connected.

  • baronnath

    Thanks! You helped me to fix my code with your example