无法在PHP的浏览器中查看下载的PDF文件

时间:2019-01-07 14:28:49

标签: php joomla

我具有下载媒体功能,可以下载所有类型的媒体文件。下面是代码

public function downloadMedia($file, $filename_direct = '', $extern = '', $exitHere = 1)
    {
        jimport('joomla.filesystem.file');

        clearstatcache();

        if (!$extern)
        {
            if (!JFile::exists($file))
            {
                return 2;
            }
            else
            {
                $len = filesize($file);
            }
        }
        else
        {
            /* Return the size of a remote url or a local file specified by $url.
                $thereturn specifies the unit returned (either bytes "", MiB "mb" or KiB
                "kb"). */
            $len = filesize($file);
        }

        $filename       = basename($file);

        $file_extension = strtolower(substr(strrchr($filename, "."), 1));
        $ctype = $this->getMime($file_extension);

        ob_end_clean();

        //  Needed for MS IE - otherwise content disposition is not used?
        if (ini_get('zlib.output_compression'))
        {
            ini_set('zlib.output_compression', 'Off');
        }

        header("Cache-Control: public, must-revalidate");
        header('Cache-Control: pre-check=0, post-check=0, max-age=0');
        header("Expires: 0");
        header("Content-Description: File Transfer");
        header("Content-Type: " . $ctype);
        header("Content-Length: " . (string) $len);
        header('Content-Disposition: attachment; filename="' . $filename . '"');

        //  set_time_limit doesn't work in safe mode
        if (!ini_get('safe_mode'))
        {
            @set_time_limit(0);
        }

        /*@readfile($file);

        if ($exitHere == 1)
        {
            exit;
        }*/

        $fp = fopen($file, "r") ;

        ob_clean();
        flush();
        while (!@feof($fp)) {
            $buff = @fread($fp, $len);
            print $buff;
        }

        exit;
    }

现在的问题是,每当我下载PDF文件并单击下载的文件时,它就会在浏览器中显示错误,例如“无法加载PDF文档。”

Error Image

仅当我在Adobe中打开文件而不是在浏览器中打开文件时,下载的PDF文件才能正确打开。

0 个答案:

没有答案