Transfer a File via PHP

Just random stuff - Nothing to see here - Move along...

Transfer a File via PHP

I had an issue come up where GoDaddy shared hosting didn’t allow outbound FTP connections for backups, etc..  Helping someone migrate away from GoDaddy, I didn’t want to download 2 GB of data and re-upload, so threw this quick script together to make it happen:

<?php
if(!@copy('http://source.com/thefile.bz2','./thefile.bz2'))
{
  $errors= error_get_last();
  echo "COPY ERROR: ".$errors['type'];
  echo "<br />\n".$errors['message'];
} else {
  echo "File copied from remote!";
}
?>

If you want to do it via FTP, then this works well for plain-text GoDaddy remote connections:

<?php
if(!@copy('ftp://UN:PW@SERVER/file/location/thefile.bz2','./thefile.bz2'))
{
  $errors= error_get_last();
  echo "COPY ERROR: ".$errors['type'];
  echo "<br />\n".$errors['message'];
} else {
  echo "File copied from remote!";
}
?>

 

Let’s say you want to transfer via FTP between two servers while on a 3rd server.

<?php
if(!@copy('ftp://UN:PW@SERVER-A/file/location/thefile.bz2','ftp://UN:PW@SERVER-B/file/location/thefile.bz2'))
{
  $errors= error_get_last();
  echo "COPY ERROR: ".$errors['type'];
  echo "<br />\n".$errors['message'];
} else {
  echo "File copied from remote!";
}
?>