Upload photos on Facebook using PHP

20 comments
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">
<html>
<head>
<title>WebSpeaks.in | Upload images to Facebook</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style>
html{
	font-family: "lucida grande",tahoma,verdana,arial,sans-serif;
}
.main{
	width:400px;
	margin:auto;
	border:2px solid #0066CC;
	color:#3B5998;
	padding:20px;
	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;
}
a{
	color:#3B5998;
}
</style>
</head>

<body>
<?php
/******************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();

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

if($_SERVER['REQUEST_METHOD'] =='POST'){
	$img = realpath($_FILES["pic"]["tmp_name"]);
	// allow uploads
	$facebook->setFileUploadSupport("http://" . $_SERVER['SERVER_NAME']);
	// add a status message
	$photo = $facebook->api('/me/photos', 'POST', 
		array(
			'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>
	</form>
</div>
</body>
</html>

20 comments

How to upload an image from url ? kindly describe that

where is the /library/facebook.php file ?

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?

hey my image is not getting uploaded.. after clicking on upload my image.. it is showing blank page...

i set my appId and Secret Id but when i fired my url then it show me not anything??help me out plz

@anish, Please try to print the exact error by enabling error printing.

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"??

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

Your demo is not working

how to view the created album on facebook page without clicking the url given by you.

how to see the album on facebook page without clicking on link given by you

Go to the facebook.php page. Cut the session_start() function in the error line and paste it in the very first line of the same page. It will work!!

Sir can you tell me how this website works http://myfbcover.com/ .Please help me out i want to make a website like this but way more feature than this . Thanks

Sie, can you please tell me how this website works http://myfbcover.com/ . Please it would be great of you

m selling my codes of my profile cover site.
http://kiing.cc/
for 8$ only
contact me.
http://facebook.com/i.lob.ewh

Hi, thanks for the tutorial. I have a question, how can I set image to auto upload to user wall instead of uploading it?thanks

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 ?

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?

Thanks

We would love to hear from you...

back to top