PHP之FTP、SFTP上传下载
对接三方时遇见接口采用文件加密后使用FTP或SFTP传输,使用PHP对接。所以产生了本篇文章(基于TP3.2)。本篇文章包括以下几点:
- 基本代码
- 使用方法
- PHP之SFTP扩展库编译安装注意事项
一、PHP之FTP、SFTP代码,拷贝直接用
FTP及SFTP官方文档详见:
http://php.net/manual/en/book.ftp.php
http://php.net/manual-lookup.php?pattern=SFTP&scope=quickref
以下为本文最核心代码,拷贝直接使用:
1、FTP
- TP3.2存放类文件目录下(Common/Org/)新建php文件:ftp.class.php
<?php
class ftp
{
public $off;
public $conn_id;
function __construct($FTP_HOST,$FTP_PORT,$FTP_USER,$FTP_PASS)
{
$this->conn_id = @ftp_connect($FTP_HOST,$FTP_PORT) or die("FTP服务器连接失败");
@ftp_login($this->conn_id,$FTP_USER,$FTP_PASS) or die("FTP服务器登陆失败");
@ftp_pasv($this->conn_id,1);
}
function up_file($path,$newpath,$type=true)
{
if($type) $this->dir_mkdirs($newpath);
$this->off = @ftp_put($this->conn_id,$newpath,$path,FTP_BINARY);
if(!$this->off) echo "文件上传失败,请检查权限及路径是否正确!";
}
function move_file($path,$newpath,$type=true)
{
if($type) $this->dir_mkdirs($newpath);
$this->off = @ftp_rename($this->conn_id,$path,$newpath);
if(!$this->off) echo "文件移动失败,请检查权限及原路径是否正确!";
}
function copy_file($path,$newpath,$type=true)
{
$downpath = "c:/tmp.dat";
$this->off = @ftp_get($this->conn_id,$downpath,$path,FTP_BINARY);
if(!$this->off) echo "文件复制失败,请检查权限及原路径是否正确!";
$this->up_file($downpath,$newpath,$type);
}
function del_file($path)
{
$this->off = @ftp_delete($this->conn_id,$path);
if(!$this->off) echo "文件删除失败,请检查权限及路径是否正确!";
}
function dir_mkdirs($path)
{
$path_arr = explode('/',$path);
$file_name = array_pop($path_arr);
$path_div = count($path_arr);
foreach($path_arr as $val)
{
if(@ftp_chdir($this->conn_id,$val) == FALSE)
{
$tmp = @ftp_mkdir($this->conn_id,$val);
if($tmp == FALSE)
{
echo "目录创建失败,请检查权限及路径是否正确!";
exit;
}
@ftp_chdir($this->conn_id,$val);
}
}
for($i=1;$i<=$path_div;$i++)
{
@ftp_cdup($this->conn_id);
}
}
function close()
{
@ftp_close($this->conn_id);
}
}
?>
使用方法:只说明上传、下载。其他请参考官方文档及注释
import("Common/Org/ftp");
$ftp = new \ftp("host",port,"user","passwd");
$ftp->up_file("localpath","remotePath");
$ftp->copy_file("remotePath","localpath");
$ftp->close();
2、SFTP
- TP3.2存放类文件目录下(Common/Org/)新建php文件 sftp.class.php
<?php
class sftp{
private $config =NULL ;
private $conn = NULL;
private $use_pubkey_file= false;
public function init($config){
$this->config = $config ;
}
public function connect(){
$methods['hostkey'] = $this->use_pubkey_file ? 'ssh-rsa' : [] ;
$this->conn = ssh2_connect($this->config['host'], $this->config['port'], $methods);
if($this->use_pubkey_file){
$rc = ssh2_auth_pubkey_file($this->conn,$this->config['user'],$this->config['pubkey_file'],$this->config['privkey_file'],$this->config['passphrase']);
}else{
$rc = ssh2_auth_password( $this->conn, $this->config['user'],$this->config['passwd']);
}
return $rc ;
}
public function download($remote, $local){
return ssh2_scp_recv($this->conn, $remote, $local);
}
public function upload($remote, $local,$file_mode=0664){
return ssh2_scp_send($this->conn, $local, $remote, $file_mode);
}
public function remove($remote){
$sftp = ssh2_sftp($this->conn);
$rc = false;
if (is_dir("ssh2.sftp://{$sftp}/{$remote}")) {
$rc = false ;
$rc = ssh2_sftp_rmdir($sftp, $remote);
} else {
$rc = ssh2_sftp_unlink($sftp, $remote);
}
return $rc;
}
}
?>
使用方法:只说明上传、下载。其他请参考官方文档及注释
import("Common/Org/sftp");
$config = array("host"=>"host","user"=>"","port"=>"","passwd"=>"");
$handle = new \sftp();
$handle->init($config);
$rc = $handle->connect();
dump($handle->upload("remotePath","localPath"));exit;
dump($handle->download("remotePath","localPath"));exit;
PHP3以上就已经自带FTP扩展,如果以上FTP代码无法使用或报错,请优先检测php.ini文件是否开启ftp扩展支持。
SFTP在使用时需要安装扩展,以下为扩展安装简述。没有安装扩展可能会出现以下问题:

如果遇见这个问题时,那就是需要安装扩展了。
PHP之SFTP扩展库编译安装注意事项
1、下载文件:
wget http:
wget http:
2、安装 libssh2 在安装 SS2
tar -zxvf libssh2-1.4.2.tar.gz
cd libssh2-1.4.2
./configure --prefix=/usr/local/libssh2
make
make test
make install
3、SSH安装
tar -zxvf ssh2-0.12.tgz
cd ssh2-0.12
phpize(个人目录不同,例如:/usr/local/php/bin/phpize)
./configure --prefix=/usr/local/ssh2 --with-ssh2=/usr/local/libssh2 --with-php-config=/usr/local/php/bin/php-config
make
make test
make install
安装完成后会在 /usr/local/php/lib/php/extensions目录下生成一些文件,可以不管
剩下就是修改php.ini配置文件
找到php.ini文件,打开后加入:
extension=ssh2.so
安装过程中可能会出现编译问题,不同问题对应不同解决方式,一般会遇见gcc及openssl找不到问题。原因是安装这个扩展时没有正确安装,找到路径即可。