php ftps,使用PHP Curl的FTPS获得部分下载

我在使用带有隐式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%的时间结果是部分下载的文件之外,这几乎完美无缺.然后我检查远程服务器,并且能够验证文件确实存在于其中 – 再次尝试脚本,它总是在第二次尝试时获取整个文件.

什么会导致像这样使用卷曲的间歇性问题?我的下一步行动是实现某种校验和并继续下载尝试,直到所有的哈希值,但这感觉更像是一个草率的解决方案而不是真正的解决方案,并且知道问题的实际根源会很好.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值