使用 PHP 如何强制下载带有随机文件名和类型的文件

我在这里看到了一些已回答的问题以强制下载文件,但没有一个问题是文件名和类型未知或可能有所不同。此外,仅在单击文件链接时才强制下载(而不是在页面加载/刷新时,这是我尝试失败期间发生的情况)。


我在 Web 服务器上有一个文件夹,其中可以包含一些具有随机文件名的不同文件类型。允许的文件类型有:doc、docx、pdf、jpg、jpeg、png 和 gif(但以后会添加更多)。


文件名由使用不同 php 文件上传文件的用户确定。


用户浏览器仅下载 doc 和 docx。


其他显示在浏览器中。


我曾试图让用户右键单击该文件来下载它,但这被置若罔闻。


显示文件的代码很简单。


<?php


    $dir = opendir('uploads/');


    echo '<ul>';

    while ($read = readdir($dir))

    {

        if ($read!='.' && $read!='..')

        {       


            echo '<li><a href="uploads/'.$read.'">'.$read.'</a></li>';


        }

    }


    echo '</ul>';


    closedir($dir);     


?>  

我想要的是在他们单击链接或链接右侧的单独下载按钮时强制启动下载对话框。


这很容易使用 php 吗?


慕桂英3389331
浏览 122回答 2
2回答

喵喵时光机

<a href="downloader.php?file=filename.extension">filename</a>然后在downloader.php文件中<?php&nbsp; &nbsp; ignore_user_abort(true);&nbsp; // prevents script termination&nbsp; &nbsp; set_time_limit(0); // prevent time out&nbsp; &nbsp; $file =&nbsp; isset($_GET['file']) ? $_GET['file'] : ''; //get filename&nbsp; &nbsp; if ($file) {&nbsp; &nbsp; &nbsp; &nbsp; $path_info = pathinfo($_GET['file']);&nbsp; &nbsp; &nbsp; &nbsp; $file_name = $path_info['basename'];&nbsp; &nbsp; &nbsp; &nbsp; $dir = "uploads";&nbsp; //directory&nbsp; &nbsp; &nbsp; &nbsp; $path_to_file = $dir.'/'.$file_name; //full path&nbsp; &nbsp; &nbsp; &nbsp; if(!file_exists($path_to_file)) {&nbsp; // check if file exist or terminate request&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;exit('the file does not exist');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(!is_readable($path_to_file)) { //check if file is readable from the directory&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; exit("security issues. can't read file from folder");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; // set download headers for file&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$finfo = finfo_open(FILEINFO_MIME_TYPE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;header('Content-Type: ' . finfo_file($finfo, $path_to_file));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$finfo = finfo_open(FILEINFO_MIME_ENCODING);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;header('Content-Transfer-Encoding: ' . finfo_file($finfo, $path_to_file));&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;`header('Content-disposition: attachment; filename="' .` basename($path_to_file) . '"');&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; readfile($path_to_file); // force download file with readfile()&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; exit('download paramater missing');&nbsp; &nbsp; }?>

吃鸡游戏

首先创建一个将接受参数的下载处理程序文件。我将其命名为 download.php下载.php<?phpignore_user_abort(true);&nbsp; // prevents script terminationset_time_limit(0); // prevent time out$file =&nbsp; isset($_GET['file']) ? $_GET['file'] : ''; //get filenameif ($file) {&nbsp; &nbsp; $path_info = pathinfo($_GET['file']);&nbsp; &nbsp; $file_name = $path_info['basename'];&nbsp; &nbsp; $dir = "uploads";&nbsp; //directory&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; $path_to_file = $dir.'/'.$file_name; //full path&nbsp; &nbsp; if(!file_exists($path_to_file)) {&nbsp; // check if file exist or terminate request&nbsp; &nbsp; &nbsp; &nbsp;exit('the file does not exist');&nbsp; &nbsp; }&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if(!is_readable($path_to_file)) { //check if file is readable from the directory&nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; exit("security issues. can't read file from folder");&nbsp; &nbsp; &nbsp;}&nbsp;// set download headers for file&nbsp; &nbsp; &nbsp;$finfo = finfo_open(FILEINFO_MIME_TYPE);&nbsp; &nbsp; &nbsp;header('Content-Type: ' . finfo_file($finfo, $path_to_file));&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;$finfo = finfo_open(FILEINFO_MIME_ENCODING);&nbsp; &nbsp; &nbsp;header('Content-Transfer-Encoding: ' . finfo_file($finfo, $path_to_file));&nbsp;&nbsp; &nbsp; &nbsp;header('Content-disposition: attachment; filename="' . basename($path_to_file) . '"');&nbsp;&nbsp; &nbsp; readfile($path_to_file); // force download file with readfile()}&nbsp;else {exit('download paramater missing');}?>用法<a href="download.php?file=randomfilename.pdf">My pdf </a>希望这可以帮助。
打开App,查看更多内容
随时随地看视频慕课网APP