Php7实现文件下载,PHP7 SFTP下载文件并重命名该下载文件
SFTP類
'',//sftp服務器地址
'port' => ,//sftp服務器端口
'username' => '', //sftp服務器用戶名
'password' => '' //sftp服務器密碼
];
// 連接為NULL
private $conn = NULL;
// 初始化
public function __construct()
{
$this->connect();
}
public function connect()
{
$this->conn = ssh2_connect($this->config['host'], $this->config['port']); //資源
if(!ssh2_auth_password($this->conn, $this->config['username'], $this->config['password']))
{
echo json_encode([
'code' => 500,
'msg' => '無法在服務器進行身份驗證',
'data' => null
]);die;
}
}
// 傳輸數(shù)據(jù) 傳輸層協(xié)議,獲得數(shù)據(jù)
public function downftp($remote, $local)
{
$ressftp = ssh2_sftp($this->conn);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='."ssh2.sftp://".$ressftp."/upload/".$remote);
header('Content-Transfer-Encoding: binary');
Header("Accept-Ranges: bytes");
header('Content-Disposition: attachment; filename=' . $local);
header('Content-Type: application/octet-stream; name=' . $local);
readfile("ssh2.sftp://".$ressftp."/upload/".$remote);
/**
* 從sftp服務器上下在文件,并保存的相應的目錄
*/
//return copy("ssh2.sftp://".$ressftp."/upload/".$remote, __DIR__.'/../../public/'.$local);
//return copy("ssh2.sftp://".$ressftp."/upload/".$remote, $local);
//return ssh2_scp_recv($this->conn, $remote, $local);
}
// 傳輸數(shù)據(jù) 傳輸層協(xié)議,寫入ftp服務器數(shù)據(jù)
public function upftp( $local,$remote, $file_mode = 0777)
{
$ressftp = ssh2_sftp($this->conn);
return copy($local,"ssh2.sftp://{$ressftp}".'gongwenliuzhuan/upload/'.$remote);
}
}
下載sftp服務上的文件的接口的文件
傳相應的參數(shù)即可
如果需要檢測sftp服務器上是否有該文件,請自行處理。
500,
'msg' => 'sftpFileName is null',
'data' => null
]);die;
}
//下載重命名
//$down_name = __DIR__.'/../../../public/'.$sftp_name;
//下載文件名
$new_name = trim($_GET['new_name']);
//下載文件類型
$type = strtolower(trim($_GET['type']));
if ($new_name == ''){
echo json_encode([
'code' => 500,
'msg' => 'newFileName is null',
'data' => null
]);die;
}
if ($type == ''){
echo json_encode([
'code' => 500,
'msg' => 'fileType is null',
'data' => null
]);die;
}
$down_name = $new_name.'.'.$type;
$my_sftp->downftp($sftp_name,$down_name);
總結(jié)
以上是生活随笔為你收集整理的Php7实现文件下载,PHP7 SFTP下载文件并重命名该下载文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php redbean update,r
- 下一篇: php怎么输出以前的时间,php输出指定