PHP cURL通过隐式FTPS上传至Sharefile时连接超时求助
问题描述
我尝试使用PHP cURL通过隐式FTPS将一个2KB的TXT文件上传至Sharefile,收到以下超时错误:
Connection timed out after 30000 milliseconds
Closing connection 0
使用的代码如下:
$fp = fopen($local_file, 'rw+'); $ftp_url = 'ftps://subdomain.sharefileftp.com/'.$ftp_path; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $ftp_url); curl_setopt($ch, CURLOPT_PORT, 990); curl_setopt($ch, CURLOPT_USERPWD, $username.':'.$password); curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_TRY); curl_setopt($ch, CURLOPT_FTPSSLAUTH, CURLFTPAUTH_TLS); curl_setopt($ch, CURLOPT_UPLOAD, 1); curl_setopt($ch, CURLOPT_INFILE, $fp); curl_setopt($ch, CURLOPT_TIMEOUT, 30); $response = curl_exec($ch); curl_close($ch);
curl_getinfo返回的数组除URL外大多为空值。开启CURLOPT_STDERR后,输出的精简日志如下:
* Expire in 0 ms for 1 (transfer 0x56523a8e1070) * Expire in 1 ms for 1 (transfer 0x56523a8e1070) * TCP_NODELAY set * Trying X.X.X.X... * Expire in 200 ms for 4 (transfer 0x56523a8e1070) * Connection timed out after 30002 milliseconds * Closing connection 0
排查与修复建议
1. 修正隐式FTPS的SSL参数
隐式FTPS要求从连接建立开始就全程加密,当前使用的CURLFTPSSL_TRY仅会尝试在控制连接启用SSL,不符合隐式FTPS的要求,需改为:
curl_setopt($ch, CURLOPT_FTP_SSL, CURLFTPSSL_ALL);
2. 强制启用被动模式
FTPS上传基本依赖被动模式,curl默认可能未正确开启,添加以下配置:
curl_setopt($ch, CURLOPT_FTPPORT, '-'); // 强制被动模式 // 若仍有问题,可禁用EPRT/EPSV curl_setopt($ch, CURLOPT_FTP_USE_EPRT, false); curl_setopt($ch, CURLOPT_FTP_USE_EPSV, false);
3. 验证网络连通性
从部署代码的服务器测试目标端口的连通性:
telnet subdomain.sharefileftp.com 990
如果无法连通,需检查服务器防火墙/安全组是否放行990端口,以及Sharefile端是否将服务器IP加入白名单。
4. 优化超时设置与错误捕获
延长超时时间并添加错误捕获逻辑,便于定位问题:
curl_setopt($ch, CURLOPT_TIMEOUT, 60); // 延长超时至60秒 // 执行后检查错误 if(curl_errno($ch)){ echo 'CURL错误: ' . curl_error($ch); }
5. 调整文件句柄模式
上传仅需读取文件权限,使用二进制读模式避免换行符等格式问题:
$fp = fopen($local_file, 'rb');
6. 指定SSL版本
Sharefile的FTPS可能要求特定SSL版本,尝试强制使用TLSv1.2:
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2);
内容的提问来源于stack exchange,提问作者Jack Booth
相关产品推荐
相关产品推荐

