Sizetrac

FTP Upload File

Need to transfer files via FTP in some automated or scheduled manner. It is actually quite easy to do using FTP. Following is a simplistic PHP FTP example to get you started.

Replace 'hostname', 'username', and 'password' with real FTP credentials, then we try and connect to the FTP server.

//FTP CONNECT

$connect = ftp_connect('hostname');

$login = ftp_login($connect, 'username', 'password');

if an error occurred connecting to the FTP server, then we can stop here:

//CONNECTION ERROR

if ((!$connect) || (!$login)) {
   
    echo "FTP Connection Failed";
   
    exit;
   
}

Otherwise let's proceed and upload a file via FTP. Be sure to replace '/path/to/file.ext' with the full path to your file, as well as 'file.ext' with the filename you would like to use when you FTP up the file.

//UPLOAD THE FILE

$file = fopen('/path/to/file.ext', 'r');

$upload = ftp_fput($connect, 'file.ext', $file, FTP_ASCII);

fclose($file);

ftp_close($connect);

and finally let's perform a simple check to see if the file was uploaded successfully.

// UPLOAD ERROR

if (!$upload) {

    echo "FTP Upload Failed";
   
}

// UPLOAD SUCCESS
   
else {
   
    echo "FTP Upload Success";
   
}

  • Digg
  • TwitThis
  • del.icio.us
  • Netvouz
  • description
  • Reddit
  • Furl
  • NewsVine
  • Simpy
  • Slashdot
  • Spurl
  • StumbleUpon
  • YahooMyWeb
  • TailRank
  • Technorati
  • Facebook
  • Google
  • LinkedIn
  • Live
  • MySpace
  • Ping.fm
  • Yahoo! Buzz
  • E-mail this story to a friend!



Home | PHP | FTP Upload File