我想向用户显示我的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);
发送内容型和文件内容
$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