這篇文章主要講解了“php如何實現(xiàn)sftp上傳”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“php如何實現(xiàn)sftp上傳”吧!

目前創(chuàng)新互聯(lián)公司已為數(shù)千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站改版維護、企業(yè)網(wǎng)站設(shè)計、武川網(wǎng)站維護等服務(wù),公司將堅持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
本文操作環(huán)境:windows7系統(tǒng)、PHP7.1版,DELL G3電腦
php怎么實現(xiàn)sftp上傳?
php 實現(xiàn)SFTP上傳文件
php 實現(xiàn)sftp文件上傳完全可以用php.net 官網(wǎng)中的方式,代碼如下:
class SFTPConnection
{
private $connection;
private $sftp;
public function __construct($host, $port=22)
{
$this->connection = @ssh3_connect($host, $port);
if (! $this->connection)
throw new Exception("Could not connect to $host on port $port.");
}
public function login($username, $password)
{
if (! @ssh3_auth_password($this->connection, $username, $password))
throw new Exception("Could not authenticate with username $username " .
"and password $password.");
$this->sftp = @ssh3_sftp($this->connection);
if (! $this->sftp)
throw new Exception("Could not initialize SFTP subsystem.");
}
public function uploadFile($local_file, $remote_file)
{
$sftp = $this->sftp;
$stream = @fopen("ssh3.sftp://$sftp$remote_file", 'w');
if (! $stream)
throw new Exception("Could not open file: $remote_file");
$data_to_send = @file_get_contents($local_file);
if ($data_to_send === false)
throw new Exception("Could not open local file: $local_file.");
if (@fwrite($stream, $data_to_send) === false)
throw new Exception("Could not send data from file: $local_file.");
@fclose($stream);
}
}
try
{
$sftp = new SFTPConnection("localhost", 22);
$sftp->login("username", "password");
$sftp->uploadFile("/tmp/to_be_sent", "/tmp/to_be_received");
}
catch (Exception $e)
{
echo $e->getMessage() . "\n";
}但是在進行中遇到了一個問題, 我的php版本是 PHP 5.6.31 (cli) (built: Aug 2 2017 15:05:23) , 在執(zhí)行
$stream = @fopen("ssh3.sftp://$sftp$remote_file", 'w');fopen的時候 執(zhí)行文件 會報 "Segmentation fault" 的錯誤, 然后變成以下方式便可以解決
$stream = @fopen("ssh3.sftp://" . intval($sftp) . $remote_file, 'w');其中,在實現(xiàn)sftp上傳的時候,沒有在意上傳文件和上傳目錄的區(qū)別(例如: /upload 和 /upload/test.txt 的問題), 導(dǎo)致每次執(zhí)行php 都會報 fopen(): Unable to open ssh3.sftp://5/upload on remote host. 問題解決方法就是 認(rèn)真, 大寫的認(rèn)真
以上就是php做的, 只要登錄sftp服務(wù)器 進行查看便知道結(jié)果.
sftp 命令登錄方式:
sftp -oPort=port user@server 然后輸入密碼, 進去之后可以到相對的目錄查看文件是否存在.
感謝各位的閱讀,以上就是“php如何實現(xiàn)sftp上傳”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對php如何實現(xiàn)sftp上傳這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!
當(dāng)前文章:php如何實現(xiàn)sftp上傳
分享鏈接:http://www.jbt999.com/article40/pspgho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、ChatGPT、網(wǎng)頁設(shè)計公司、品牌網(wǎng)站設(shè)計、云服務(wù)器、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)