php - 如何显示来自 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);
解决方案
发送 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
推荐阅读
- android - 在 React Native 中查看包装器导致 Android 应用程序崩溃
- qt - 如何捕捉 Qt 保持事件
- python - Matplotlib 子图问题
- sql - 如何从数据库列中分离记录并在查询中进行比较,忽略 Oracle 中的第二个文本
- visual-studio-2017 - VS 2017 中的 VisualHG:提交解决方案中的所有项目
- python - 在 Windows 上安装 Scrapy 的问题
- docker - Docker:无法连接到 gitlab 实例
- python - Buildout 找不到 Pypi Plone 附加组件的依赖项的链接
- scala - 为什么 Visual VM 在计划线程时将线程显示为已停止?
- java - java - 如何从用逗号和'\ n'分隔的java中的控制台获取输入?