日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Php7实现文件下载,PHP7 SFTP下载文件并重命名该下载文件

發(fā)布時間:2025/3/19 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Php7实现文件下载,PHP7 SFTP下载文件并重命名该下载文件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。