php7之後無法使用ssh2_connect()函式連結ftp伺服器,找了很久之後發現CURL可以解決,寫篇文章記錄一下吧!
$localFile = '/var/www/test/a.txt';
$pathInfo = pathinfo($localFile);
$uploadFileName = $pathInfo['basename'];
$fileSize = fileSize($localFile);
$fh = fopen($localFile, 'r');//獲取檔案控制程式碼, https://php.net/curl_setopt,文件裡面也有人貢獻程式碼,但是我測試不透過,作者寫多了一個配置CURLOPT_PROTOCOLS => CURLPROTO_FTP,
$connection = 'ftp://xxxxx' . $uploadFileName;
$userPwd = 'root:password';//系統使用者名稱:系統密碼
$ch = curl_init();
curl_setopt_array(
$ch,
array(
CURLOPT_URL => $connection,
CURLOPT_USERPWD => $userPwd,
CURLOPT_UPLOAD => true,
CURLOPT_INFILE => $fh,
CURLOPT_INFILESIZE => $fileSize,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HEADER => false,
)
);
curl_exec($ch);
if(curl_errno($ch) !== CURLE_OK){
throw new Exception(curl_error($ch));
}
curl_close($ch);
本作品採用《CC 協議》,轉載必須註明作者和本文連結