简体   繁体   中英

How can I show a PDF return from sftp-get from PHPSECLIB

I want to show to the user a PDF or a PNG that is in my SFTP server. I am doing the connection with phpseclib to get the file. How can I manipulate the file to show it in the browser?

    $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); 

Send Content-Type and file content

$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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM