我在使用带有隐式ssl的ftps上的php curl检索文件时遇到问题(如下所述:
ftp_ssl_connect with implicit ftp over tls).问题是有时候 – 大概有5%的时间,我最终会部分下载.
我的课程或多或少地改编自改编自Nico Westerdale的答案,以下是相关方法:
class ftps {
private $server;
private $username;
private $password;
private $curlhandle;
public $dir = '/';
public function __construct($server, $username, $password) {
$this->server = $server;
$this->username = $username;
$this->password = $password;
$this->curlhandle = curl_init();
}
private function common($remote) {
curl_reset($this->curlhandle);
curl_setopt($this->curlhandle, CURLOPT_URL, 'ftps://' . $this->server . '/' . $remote);
curl_setopt($this->curlhandle, CURLOPT_USERPWD, $this->username . ':' . $this->password);
curl_setopt($this->curlhandle, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($this->curlhandle, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($this->curlhandle, CURLOPT_FTP_SSL, CURLFTPSSL_TRY);
curl_setopt($this->curlhandle, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS);
return $this->curlhandle;
}
public function download($filepath, $local = false) {
$filename = basename($filepath);
$remote = dirname($filepath);
if ($remote == '.') {
$remote = $this->dir;
}
if ($local === false) {
$local = $filename;
}
if ($fp = fopen($local, 'w')) {
$this->curlhandle = self::common($remote . $filename);
curl_setopt($this->curlhandle, CURLOPT_UPLOAD, 0);
curl_setopt($this->curlhandle, CURLOPT_FILE, $fp);
curl_exec($this->curlhandle);
if (curl_error($this->curlhandle)) {
return false;
} else {
return $local;
}
}
return false;
}
}
我这样使用它:
$ftps = new ftps('example.com','john_doe','123456');
$ftps->download('remote_filename','local_filename');
正如我上面提到的,除了大约5%的时间结果是部分下载的文件之外,这几乎完美无缺.然后我检查远程服务器,并且能够验证文件确实存在于其中 – 再次尝试脚本,它总是在第二次尝试时获取整个文件.
什么会导致像这样使用卷曲的间歇性问题?我的下一步行动是实现某种校验和并继续下载尝试,直到所有的哈希值,但这感觉更像是一个草率的解决方案而不是真正的解决方案,并且知道问题的实际根源会很好.