首页 > 解决方案 > 如何显示来自 PHPSECLIB 的 sftp-get 的 PDF 返回

问题描述

我想向用户显示我的 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); 

标签: phppdfphpseclib

解决方案


发送 Content-Type 和文件内容

$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

推荐阅读