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…

January 16, 2010

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

Comments has been closed for too much spam. Sorry.


I'm a software engineer, an everyday web developer and a maker. I usually build sites with PHP, within or without WordPress. I build Internet of Things with Arduino and ESP8266. I'm the founder of and and I'm actually the Chief Technical Officer of Better Days web agency.

Comments on “Copying remote files on your server with PHP”

9 thoughts

  1. Face says:

    Ouch, by doing that you are not allowed to use the facebook api any longer:

  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. 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!

  6. Giulio Pons says:

    It’s wordpress.

  7. David says:

    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

Comments are closed


Updated version of the the Facebook Connect Tutorial

I’ve written un updated version of the Facebook Connect Tutorial, you can find it in the top menu of…

October 10, 2012

How to read facebook likes count from PHP

When you add facebook like button to your site, probably, you also want to save the number of likes of…

October 8, 2012

Posting to Facebook from website with Facebook Connect

This post is part of the Facebook Connect Tutorial and it will show how to add “post to wall” function…

January 17, 2010

WP doesn’t send email? try this

Snippets for sending emails with Wordpress

February 8, 2020

Highlight text for search results in PHP

Useful code to highlight text occurences in search results or in a text. How to highlight text in a string…

September 2, 2016

How to add rel=”nofollow” to links with preg_replace()

Adding rel="nofollow" to external link is a good SEO practice.

September 22, 2015