Upload photos on Facebook using PHP

If you have strived for the uploading photos on Facebook via your site, then you are at right place. On demand of my readers I have written this article to show how to upload images on Facebook via PHP. First of all you need to create a Facebook app. I will not tell you how to do that. Get the appId and secret code of your application from facebook. This appId and secret code is necessary to connect you site to the application on Facebook. This code uses the Facebook.php class that can be downloaded from here.

Live Demo

PHP Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<title>WebSpeaks.in | Upload images to Facebook</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
font-family: "lucida grande",tahoma,verdana,arial,sans-serif;
border:2px solid #0066CC;
font-size: 11px;
-moz-border-radius: 4px 4px 4px 4px;
border-radius: 4px 4px 4px 4px;
-moz-box-shadow: 1px 1px 0 #d5d5d5;
background: none repeat scroll 0 0 #F2F2F2;
.post_but {
background: none repeat scroll 0 0 #EEEEEE;
border-color: #999999 #999999 #888888;
border-style: solid;
border-width: 1px;
color: #333333;
cursor: pointer;
display: inline-block;
font-size: 11px;
font-weight: bold;
padding: 2px 6px;
text-align: center;
text-decoration: none;

/******************Configuration options***********************/

require_once 'library/facebook.php';
$facebook = new Facebook(array(
'appId' => $appId, //your facebook application id
'secret' => $secret, //your facebook secret code
'cookie' => true

$user = $facebook->getUser();

header("Location:{$facebook->getLoginUrl(array('req_perms' => 'user_status,publish_stream,user_photos'))}");
/******************Configuration options***********************/

$img = realpath($_FILES["pic"]["tmp_name"]);
// allow uploads
$facebook->setFileUploadSupport("http://" . $_SERVER['SERVER_NAME']);
// add a status message
$photo = $facebook->api('/me/photos', 'POST',
'source' => '@' . $img,
'message' => 'This photo was uploaded via www.WebSpeaks.in'

echo '<p><a target="_blank" href="http://www.facebook.com/photo.php?fbid='.$photo['id'].'">Click here to watch this photo on Facebook.</a></p>';
<div class="main">
<p>Select a photo to upload on Facebook.</p>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data">
<p>Select the image: <input type="file" name="pic" /></p>
<p><input class="post_but" type="submit" value="Upload to my album" /></p>

Written by Arvind Bhardwaj

Arvind is a certified Magento 2 expert with more than 10 years of industry-wide experience.

Website: http://www.webspeaks.in/

27 thoughts on “Upload photos on Facebook using PHP

  1. I copied and pasted this code and set valid id and secret for variables, but when visiting the page redirections occur continuously from facebook to my server and again to facebook. Any solution?

  2. Hello Arvind,
    i simply copy this code and set my appid and secret id but its not show me any thing i print error msg but it cant.its really very important for me to run this script plz help me out.and plz tel me where this “/library/facebook.php”??

  3. My problem 🙁

    Warning: session_start() [function.session-start]: Cannot send session cookie – headers already sent by (output started at library/base_facebook.php:10) in library/facebook.php on line 37

    Warning: session_start() [function.session-start]: Cannot send session cache limiter – headers already sent by (output started at library/base_facebook.php:10) in library/facebook.php on line 37

  4. Man it doesent work this script with sdk 3.2 !

    If i use sdk 2.1 2.0 etc facebook dosent allow me to upload any pictures since its 3.2 out there !

    So what to do now ?

  5. Can I use a form to add a message or caption instead off using the canned message? So when I hit upload it takes the comment and passes it as a variable which will be posted with the picture?


  6. Hey There. I found your blog the use of msn. This
    is an extremely well written article. I'll make sure to bookmark it and come
    back to learn extra of your helpful info. Thank you for the post.
    I'll definitely return.

  7. Thank you so much for giving everyone remarkably pleasant opportunity to check tips from this web site.
    It is usually very enjoyable and also full of amusement for
    me personally and my office mates to search your blog
    at least thrice weekly to read through the fresh guides you will have.
    And of course, I'm certainly contented concerning the tremendous strategies
    you serve. Some 3 areas in this article are particularly the
    very best we've ever had.

Comments are closed.