PHP: CURL upload file to FTP/FTPS server


In the past, i posted an article PHP: upload file to FTP server
In this article, i use php-ftp module. Now i introduce some method to upload file to FTP/FTPS server using CURL.

CURL upload file to FTP server

$localfile = '/path/to/local.file';
$topath = 'file.name';

$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, 'ftp://ftp_login:password@ftp.domain.com/'.$topath);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);

or

$localfile = '/path/to/local.file';
$topath = 'file.name';

$fp = fopen($localfile, 'r');
curl_setopt($ch, CURLOPT_URL, 'ftp://ftp.domain.com/'.$topath);
curl_setopt($ch, CURLOPT_USERPWD, "ftp_login:password");
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile));
curl_exec ($ch);
$error_no = curl_errno($ch);
curl_close ($ch);

CURL upload file to FTPS server
Same as previous but add more CURL options:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FTP_SSL, true);
curl_setopt($ch, CURLOPT_VERBOSE, true);

CURL:

curl -v --ftp-ssl --insecure -T test.jpg ftp://ftp_login:password@ftp.domain.com/path/test.jpg

or

curl -v --ftp-ssl --insecure -T test.jpg ftp://ftp.domain.com/path/test.jpg --user ftp_login:password

Leave a Reply