如何从PHPSECLIB中显示sftp-get的PDF返回

问题描述 投票:1回答:1

我想向用户显示我的SFTP服务器中的PDF或PNG。我正在与phpseclib建立连接以获取文件。如何操作文件以在浏览器中显示它?

    $key = new RSA();
    $key->loadKey(file_get_contents('*******'));
    $sftp = new SFTP($fsock);
    if (!$sftp->login('*******', $key)) {
        exit('Bad credentials!');
    } 


    $path = "******";
    $filename = $result[0]->filefinalname ;
    $file = $sftp->get($path . $filename); 
php pdf phpseclib
1个回答
2
投票

发送内容类型和文件内容

$file = '/path/to/temp/dir/'.uniqid(rand(), true);  //temp name
file_put_contents($file,$sftp->get($path . $filename)); // save temp file

if (exif_imagetype($file))  //if this image
    header("Content-Type: image/png"); //type file png
else
    header("Content-type:application/pdf"); //type file pdf

header('Content-Length: ' . filesize($file)); //send size
readfile($file); //send file
unlink($file); //delete temp file
© www.soinside.com 2019 - 2024. All rights reserved.