如何直接下载ftp_fget()生成的tmpfile

时间:2016-11-23 03:10:47

标签: php file ftp

我有一个文件列表,不同的类型,当你点击例如pdf时,它将通过查看器显示pdf。

我尝试添加一个条件,当它不是pdf时直接下载它。但我无法弄明白。

它位于本地文件上,但如何将该本地文件下载到我的计算机上?它下载了viewer.php而不是有问题的文件,在viewer.php中有一些ascii写在其中。

VIEWER.PHP     

// Si la variable $_GET['strPDF'] est définie ->
if ( isset($_GET['strPDF']) ) {
    // 'Décryption' de la variable ->
    $strPDF = hex2bin($_GET['strPDF']);
    // Connexion au serveur FTP et récupération du fichier demandé ->
    $streamFTP = ftp_connect($strFTP_Hote, $intFTP_Port, $intFTP_tOut);
    if ( ftp_login($streamFTP, $strUser, $strPass) ) {

        $tmpHandle = tmpfile(); 
        ftp_fget($streamFTP, $tmpHandle, $strPDF, FTP_BINARY);
        rewind($tmpHandle); 

        $contentType = mime_content_type($tmpHandle);

        $re = '/(\/pdf)/';
        preg_match($re, $contentType, $matches);
        if(count($matches) > 1){
            header('Content-type: ' . $contentType);
            echo stream_get_contents($tmpHandle);
        }
        else{
            header('Content-Description: File Transfer');
            header('Content-Type: application/force-download');
            // // header("Content-Disposition: attachment; filename=\"" . basename() . "\";");
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            // // header('Content-Length: ' . filesize());


            // it downloads viewer.php with some sort of ascii in it. 
            echo stream_get_contents($tmpHandle);
            exit;
        }

        fclose($tmpHandle);
    }
}

1 个答案:

答案 0 :(得分:1)

使用create table #xref(yearofemp int) insert into #xref values(2009) insert into #xref values(2010) insert into #xref values(2011) insert into #xref values(2012) insert into #xref values(2013) insert into #xref values(2014) insert into #xref values(2015) insert into #xref values(2016) create table #emp(userid int,create_timestamp datetime,Last_login datetime) insert into #emp values(10,'2009-06-18','20161029') insert into #emp values(11,'2010-07-01','20110101') insert into #emp values(12,'2011-10-01','20150101') insert into #emp values(13,'2012-12-01','20161101') select a.yearofemp as Year_No ,COUNT(a.userid) as Count_of_emp from ( select #emp.userid ,#xref.yearofemp from #emp inner join #xref on yearofemp between year(#emp.create_timestamp ) and year(#emp.Last_login) )a group by a.yearofemp order by a.yearofemp 标题。

Content-Disposition

您可能希望从header("Content-Disposition: attachment; filename=\"my.pdf\";");

中提取真实姓名
$strPDF

您还应添加header("Content-Disposition: attachment; filename=\"" . basename($strPDF) . "\";"); 标题。