尝试这样做,你可以下载。用php中的sftp

时间:2014-10-16 17:42:41

标签: php sftp phpseclib

我尝试这样做,我可以下载文件,但我有一个问题,不知道如何解决它,你能帮我找到错误吗?

什么都没发生。并帮助我实现这一目标。我不知道如何解决这个问题所以需要我能得到的所有帮助。

<?php

require("configuration.php");
require("include.php");

require_once('./libs/phpseclib/SFTP.php');
require_once("./libs/phpseclib/Crypt/AES.php");


if(!isset($_SESSION['clientid']))
{
    //DON'T KNOW HOW THE REQEUSTOR IS!!
    die();
}

$clientid = $_SESSION['clientid'];

$serverid = '';
$extendedPath = '';
$action = '';

if(!isset($_GET['serverid']) or !isset($_GET['path']) or !isset($_GET['action']))
{
    die();
}


$serverid = $_GET['serverid'];
$extendedPath = $_GET['path'];
$action = $_GET['action'];

$boxDetailsSQL = sprintf("SELECT box.boxid, box.ip, box.login, box.password, box.sshport, srv.path
                            FROM %sbox box
                            JOIN %sserver srv ON box.boxid = srv.boxid
                            JOIN %sgroupMember grpm ON (grpm.groupids LIKE CONCAT(srv.groupid, ';%%')
                                                      OR grpm.groupids LIKE CONCAT('%%;', srv.groupid, ';%%'))

                            WHERE srv.serverid = %d
                            AND grpm.clientid = %d;", DBPREFIX, DBPREFIX, DBPREFIX, $serverid, $clientid);

$boxDetails = mysql_query($boxDetailsSQL);
$rowsBoxes = mysql_fetch_assoc($boxDetails);

$aes = new Crypt_AES();
$aes->setKeyLength(256);
$aes->setKey(CRYPT_KEY);

$sftp= new Net_SFTP($rowsBoxes['ip'], $rowsBoxes['sshport']);

if(!$sftp->login($rowsBoxes['login'], $aes->decrypt($rowsBoxes['password'])))
{
    echo 'Failed to connect';
    die();
}


//ACTION SELECTOR
if($action == 'list')
{
    getlist($rowsBoxes, $extendedPath, $sftp);
}   

if($action == 'fileUpload')
{
    fileUpload($rowsBoxes, $extendedPath, $sftp);

}   

if($action == 'download')
{
    delete($rowsBoxes, $extendedPath, $sftp);   
}   

//ACTION FUNCTIONS

function download($rowsBoxes, $extendedPath, $sftp)
{
    $remoteFile = dirname($rowsBoxes['path']).'/'.trim($extendedPath.'/');
    $downloadfile $sftp->put($remoteFile);

    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($downloadfile));
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($downloadfile));
    readfile($downloadfile);

}

这段代码我需要帮助才能修复。

function download($rowsBoxes, $extendedPath, $sftp)
{
$remoteFile = dirname($rowsBoxes['path']).'/'.trim($extendedPath.'/');
$downloadfile $sftp->put($remoteFile);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($downloadfile));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($downloadfile));
readfile($downloadfile);

}

1 个答案:

答案 0 :(得分:0)

您正在拨打getListfileUploaddelete,具体取决于$ _GET [&#39;操作&#39;]的价值,但您需要根本没有打电话给download。您已定义该功能,但您未在代码段中调用它。并在您的下载功能......

$downloadfile $sftp->put($remoteFile);

你可能应该这样做:

$downloadfile = $sftp->get($remoteFile);

此外,不是filesize($downloadfile)执行strlen($downloadfile)而不是readfile($downloadfile)执行echo $downloadfile;。如果要保存到本地文件,请执行$sftp->get($remoteFile, $downloadfile);

更新的代码:

function download($rowsBoxes, $extendedPath, $sftp)
{
$remoteFile = dirname($rowsBoxes['path']).'/'.trim($extendedPath.'/');
$downloadfile = $sftp->get($remoteFile);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($downloadfile));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . strlen($downloadfile));
echo $downloadfile;

}

如果你想做一些像显示图像这样的事情:

function download($rowsBoxes, $extendedPath, $sftp)
{
$remoteFile = dirname($rowsBoxes['path']).'/'.trim($extendedPath.'/');
$downloadfile = $sftp->get($remoteFile);

$file_extension = strtolower(substr(strrchr($filename,"."),1));

switch( $file_extension ) {
    case "gif": $ctype="image/gif"; break;
    case "png": $ctype="image/png"; break;
    case "jpeg":
    case "jpg": $ctype="image/jpg"; break;
    default:
}

header('Content-type: ' . $ctype);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
echo $downloadfile;

}