不是用codeigniter下载图像和pdf文件


Not force_download with images and pdf files in codeigniter

我有一个关于force_download函数的问题,我在网站上有一个上传表单,我使用这个函数来下载我上传的数据,它的工作

public function download($file)
    {
        force_download('./uploads/'.$file, NULL);
    }

但是你知道pdf,png,jpg文件可以直接在导航器中看到,如果你想,你不需要下载它,但是如果我使用这个功能,所有的文件都下载了,我怎么能得到它?

我尝试使用直接链接到我的上传文件夹,但这是可能的,因为我有一个。htaccess文件拒绝访问,以防止登录用户只能下载一些东西。

正如我已经写过的,在下载/预览代码之前检查if elseif else甚至更好的switch case块。例如:

public function download($file)
{
    //get the file extension
    $info = new SplFileInfo($file);
    //var_dump($info->getExtension());
    switch ($info->getExtension()) {
        case 'pdf':
        case 'png':
        case 'jpg':
            $contentDisposition = 'inline';
            break;
        default:
            $contentDisposition = 'attachment';
    }
    if (file_exists($file)) {
        header('Content-Description: File Transfer');
        header('Content-Type: application/pdf');
        // change inline to attachment if you want to download it instead
        header('Content-Disposition: '.$contentDisposition.'; filename="'.basename($file).'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        readfile($file);
    }
    else echo "Not a file";
}