Jan 16 2010

Copying remote files on your server with PHP

Category: Facebook,PhpGiulio Pons @ 2:22 pm

Here is a function that let you copy remote files (remote images, for example) on your web site. This can be done also with CURL, but this function it’s enaugh if you’re not making some real splider job:

function copyFile($url,$filename){
	$file = fopen ($url, "rb");
	if (!$file) return false; else {
		$fc = fopen($filename, "wb");
		while (!feof ($file)) {
			$line = fread ($file, 1028);
			fwrite($fc,$line);
		}
		fclose($fc);
		return true;
	}
}

As I wrote on the Facebook Connect Tutorial, this function can be added in your Facebook Connect page to save the user picture on your server, where you already have the users’ avatars of your community.

First, you have to retrive also the picture from Facebook, so, change the call to facebook’s users_getInfo method, near line 68 of the Facebook Connect Tutorial code:

// you need also the picture, not only the name:
$user_details=$fb->api_client->users_getInfo($fb_user, array('last_name','first_name','pic_square'));

Then, after the insert of the new user on your database (after line 100 in the tutorial), copy its picture where you need:

// get the id of the user just created
// and copy the file on your avatar_imgs directory
// (this code supposes that you store the avatars with
// the id of the user on the database)
$id_new_user = mysql_insert_id();
$avatar_new = "avatar_imgs/" . $id_new_user . ".jpg";
copyFile( $user_details[0]['pic_square'] , $avatar);
Share

Related posts:

  1. Posting to Facebook from website with Facebook Connect
  2. PHP code to check if remote mp3 exists
  3. Load File into a String
  4. How many users are connected?
  5. Calculate dir size recursively with PHP (and count files)

Tags: , , ,

6 Responses to “Copying remote files on your server with PHP”

  1. Face says:

    Ouch, by doing that you are not allowed to use the facebook api any longer: http://developers.facebook.com/policy/

  2. admin says:

    Mmm… I’ve just read the policies and it seems that you are right. Well this is just a tutorial.

  3. Fiend says:

    Sure this sounds like a shady question… but how would they know?

    Just curious im not going to use it cause i dont wanna get banned just …. curious.

  4. admin says:

    I think they can’t discover it with software.
    But it doesn’t matter how… it’s a rule.

  5. Facebook Connect how to PHP | Open source Tutorials News and how to's says:

    [...] copy the avatar from facebook to your server: it’s easy: note, this is not approved by Facebook policies! b. handle redirect to the correct page: if you [...]

  6. Facebook Connect Tutorial เชื่อมต่อเวปเรากับ facebook ใน 8 นาที « สอนเขียนโปรแกรม says:

    [...] a. copy the avatar from facebook to your server: it’s easy: note, this is not approved by Facebook policies! b. handle redirect to the correct page: if you have the login in every page you can modify this code to let the user go back to the correct page after the login process. c. handle duplicated accounts: sometimes a user that connect with facebook already has an account on your site, so you will have two users for the same person on your db. That could be a problem, we will explain why and how to solve the problem. d. handle registration completed: registration is completed when a user give you every data you consider mandatory, such as the email. This script already handles the flag fl_facebook, but in a further post we will explain how to modify your scripts to handle this part of the process. e. use facebook connect functions to post to users wall on facebook: this will increase traffic to your site. [...]

Leave a Reply