Skip to content

Instantly share code, notes, and snippets.

@pierrejoye
Created April 21, 2011 11:58
Show Gist options
  • Save pierrejoye/934311 to your computer and use it in GitHub Desktop.
Save pierrejoye/934311 to your computer and use it in GitHub Desktop.
Upload a dir content asynchronously to a ftps using curl and php
<?php
$src_dir = '/some/local/directory';
$curl = array();
$ftpserver = 'ftp.example.com';
$ftp_path = "/remotedirectory";
$ftp_user = 'johndo';
$ftp_password = 'foobar';
$mh = curl_multi_init();
$files = glob($src_dir . '/*.zip');
foreach ($files as $i => $local_file) {
$ch = $curl[$i] = curl_init();
$fp = fopen($local_file, "rb");
$local_file = basename($local_file);
$remoteurl = "ftps://$ftp_user:$ftp_password@$ftpserver/$ftp_path/$local_file";
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_URL, $remoteurl);
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch , CURLOPT_USERPWD, $ftp_user . ':' . $ftp_password);
curl_multi_add_handle ($mh, $ch);
}
do {
$n=curl_multi_exec($mh,$active);
} while ($active);
foreach ($curl as $ch) {
$res[$i]=curl_multi_getcontent($ch);
curl_multi_remove_handle($mh, $ch);
echo "error: . " curl_error($ch) . "\n";
curl_close($ch);
}
curl_multi_close($mh);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment