Copying remote files on your server with PHP

UPDATE: This function has been inserted in the Mini Bots PHP Class

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);

UPDATE
Comments has been closed for too much spam. Sorry.

  1. Updated version of the the Facebook Connect Tutorial
  2. Facebook Connect Tutorial, new version
  3. Facebook Connect Tutorial
  4. Posting to Facebook from website with Facebook Connect
  5. PHP code to check if remote mp3 exists

9 comments

  1. Fiend

    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.

  2. Pingback: Facebook Connect how to PHP | Open source Tutorials News and how to's
  3. Pingback: Facebook Connect Tutorial เชื่อมต่อเวปเรากับ facebook ใน 8 นาที « สอนเขียนโปรแกรม
  4. free car design games

    Hey would you mind sharing which blog platform you’re working with? I’m planning to start my own blog in the near future but I’m having a hard time deciding between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design seems different then most blogs and I’m
    looking for something unique.
    P.S Apologies for getting off-topic but I had to ask!

  5. David

    I really can’t see any violation of privacy .. because its the smaller image .. it’s public .
    But.. it would be nice, and maybe better.. If you get and save the url of it ..
    Or.. Use graph URL with user ID to get it.. Doing this, that embeded image will be very updated :D
    and by the way, of course they track it.. Bu