In this tutorial, I will provide code which will send files to an FTP Server with cURL using PHP.
cURL has many advantages of standard FTP, one of which is better control over error handling.

 

Below is complete code to send a single file to a remote FTP server.

The connection time-out has been set at 10 seconds, and the transfer time-out also set at 10 seconds. This is usually enough time to wait for a response. If a file transfer fails, the script will try to sent the file again a further two times. A log is written out with results of the transfer.

Please refer to the in-line comments for a description of what each part does.

 

<?php
// create constants
define('FTP_SERVER', 'ftp.somedomain.com');
define('FTP_USER', 'ftpusername');
define('FTP_PASS', 'ftppassword');

// create curl handler
$ch = curl_init();
// set connection timeout to 10 seconds
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
// set transfer timeout to 10 seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

function sendFile($filepath,$filename,$mode,$retry=0) {
  // enable function access to curl handler
  global $ch;
  // build the connection string
  $remoteurl = 'ftp://'.FTP_USER.':'.FTP_PASS.'@'.FTP_SERVER.'/'.$filename; 
  // open the file
  $fp = fopen($filepath.$filename, "r$mode");
  // set connection
  curl_setopt($ch, CURLOPT_URL, $remoteurl);
  // enable upload
  curl_setopt($ch, CURLOPT_UPLOAD, 1);
  // set file contents
  curl_setopt($ch, CURLOPT_INFILE, $fp);
  // set file size
  curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filepath.$filename));
  // execute the command
  $exec = curl_exec($ch);

  // check for errors
  if(curl_getinfo($ch) <> 226 && $retry < 3) {
    // filed, try again upto 3 times
    sendFile($filepath,$filename,$mode,$retry++);
  } elseif(curl_getinfo($ch) <> 226 && $retry == 3) {
    // all tries have failed
    return false;
  } else {
    // success
    return true;
  }

}

// set file path
$filepath = "/path/to/file/";
$filename = "somefile.jpg";
$mode = "b"; //"b" for binary or "t" for text

if(!sendFile($filepath,$filename,$mode)) {
  echo "failed to transfer $filepath $filename \n";  
}

?>

As mentioned, the above will send a single file. Now here is the same code again, but this time it will read a local directory sending each file.

 

<?php 

// create constants
define('FTP_SERVER', 'ftp.somedomain.com');
define('FTP_USER', 'ftpusername');
define('FTP_PASS', 'ftppassword');

// create curl handler
$ch = curl_init();
// set connection timeout to 10 seconds
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
// set transfer timeout to 10 seconds
curl_setopt($ch, CURLOPT_TIMEOUT, 10);

function sendFile($filepath,$filename,$mode,$retry=0) {
  // enable function access to curl handler
  global $ch;
  // build the connection string
  $remoteurl = 'ftp://'.FTP_USER.':'.FTP_PASS.'@'.FTP_SERVER.'/'.$filename; 
  // open the file
  $fp = fopen($filepath.$filename, "r$mode");
  // set connection
  curl_setopt($ch, CURLOPT_URL, $remoteurl);
  // enable upload
  curl_setopt($ch, CURLOPT_UPLOAD, 1);
  // set file contents
  curl_setopt($ch, CURLOPT_INFILE, $fp);
  // set file size
  curl_setopt($ch, CURLOPT_INFILESIZE, filesize($filepath.$filename));
  // execute the command
  $exec = curl_exec($ch);

  // check for errors
  if(curl_getinfo($ch) <> 226 && $retry < 3) {
    // filed, try again upto 3 times
    sendFile($filepath,$filename,$mode,$retry++);
  } elseif(curl_getinfo($ch) <> 226 && $retry == 3) {
    // all tries have failed
    return false;
  } else {
    // success
    return true;
  }

}

// set file path
$filepath = "/path/to/file/";
$mode = "b"; //"b" for binary or "t" for text

$dir = @opendir($filepath);
while ($file = @readdir($dir)) {
  if($file == "." || $file == "..") {
    // not files, ignore
  } else {

    if(!sendFile($filepath,$file,$mode)) {
      echo "failed to transfer $filepath $filename \n";  
    }

  }
}
?>

Tags: , , ,