Copy Remote File Into Local Directory

October 19, 2006 | PHP

Here is a little function used to copy file from remote directory (web server) into our local directory.

/*
This function used to copy remote file into local disk
Example: copyFile("http://test-server.com/file/movie.mpg","myfolder/");
*/
function copyFile($url,$dirname){
@$file = fopen ($url, "rb");
if (!$file) {
echo "Failed to copy $url !
";
return false;
}
else {
$filename = basename($url);
$fc = fopen($dirname."$filename", "wb");
while (!feof ($file)) {
$line = fread ($file, 1028);
fwrite($fc,$line);
}
fclose($fc);
echo "File $url saved to PC!
";
return true;
}
}
?>

This is very basic function. Be carefull, it will replace any existed file with the same name.

 

1 comment

This post has one comment

  1. jhj 20/10/2009 at 19:28

    not work -_-

 

Leave a comment

Allowed tags are: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


 



Switch to our mobile site