1

Hello friends I have one problem about force_download function, I have an upload form on the website and I am using this function to download the data that I upload and it´s works

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

but you know that pdf,png,jpg files can be directly seen in the navegator if you want you needn´t to download it but if I use this function all files are downloaded, how I could to get it?

I try to use the direct link to my upload folder but it can be possible because I have a .htaccess file that denied the access to prevent that log in users only can download something.

4

1 回答 1

1

正如我已经写的那样, 在下载/预览代码之前检查文件扩展名,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";
}
于 2016-10-02T12:01:03.233 回答