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

With PHP how to force download of file with randon file name and type

我在这里看到了一些强制下载文件的已回答问题,但 none 文件名和类型未知或可能不同。 此外,只有在单击文件的 link 时才会强制下载(而不是在页面 load/refresh 上,这是我尝试失败时发生的情况)。

我在 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);     

?>  

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

使用 php 可以轻松实现吗?

首先创建一个下载处理程序文件,它将接受参数。

我就叫它download.php

download.php

<?php
ignore_user_abort(true);  // prevents script termination
set_time_limit(0); // prevent time out

$file =  isset($_GET['file']) ? $_GET['file'] : ''; //get filename

if ($file) {
    $path_info = pathinfo($_GET['file']);
    $file_name = $path_info['basename'];
    $dir = "uploads";  //directory
    
    $path_to_file = $dir.'/'.$file_name; //full path

    if(!file_exists($path_to_file)) {  // check if file exist or terminate request

       exit('the file does not exist');
    }
    
    if(!is_readable($path_to_file)) { //check if file is readable from the directory
     
      exit("security issues. can't read file from folder");

     }
 
// set download headers for file

     $finfo = finfo_open(FILEINFO_MIME_TYPE);
     header('Content-Type: ' . finfo_file($finfo, $path_to_file));
  
     $finfo = finfo_open(FILEINFO_MIME_ENCODING);
     header('Content-Transfer-Encoding: ' . finfo_file($finfo, $path_to_file)); 

     header('Content-disposition: attachment; filename="' . basename($path_to_file) . '"'); 

    readfile($path_to_file); // force download file with readfile()
} 

else {

exit('download paramater missing');
}
?>

用法

<a href="download.php?file=randomfilename.pdf">My pdf </a>

希望对您有所帮助。

<a href="downloader.php?file=filename.extension">filename</a>

然后在downloader.php文件

<?php

    ignore_user_abort(true);  // prevents script termination
    set_time_limit(0); // prevent time out

    $file =  isset($_GET['file']) ? $_GET['file'] : ''; //get filename

    if ($file) {
        $path_info = pathinfo($_GET['file']);
        $file_name = $path_info['basename'];
        $dir = "uploads";  //directory

        $path_to_file = $dir.'/'.$file_name; //full path

        if(!file_exists($path_to_file)) {  // check if file exist or terminate request

           exit('the file does not exist');
        }

        if(!is_readable($path_to_file)) { //check if file is readable from the directory

          exit("security issues. can't read file from folder");

         }

    // set download headers for file

         $finfo = finfo_open(FILEINFO_MIME_TYPE);
         header('Content-Type: ' . finfo_file($finfo, $path_to_file));

         $finfo = finfo_open(FILEINFO_MIME_ENCODING);
         header('Content-Transfer-Encoding: ' . finfo_file($finfo, $path_to_file)); 

         `header('Content-disposition: attachment; filename="' .` basename($path_to_file) . '"'); 

        readfile($path_to_file); // force download file with readfile()

    } else {

        exit('download paramater missing');
    }

?>