为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕桂英3389331 2022-11-12 13:40:32
我在这里看到了一些已回答的问题以强制下载文件,但没有一个问题是文件名和类型未知或可能有所不同。此外,仅在单击文件链接时才强制下载(而不是在页面加载/刷新时,这是我尝试失败期间发生的情况)。我在 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 吗?
查看完整描述

2 回答

?
喵喵时光机

TA贡献1846条经验 获得超7个赞

<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');

    }


?>


查看完整回答
反对 回复 2022-11-12
?
吃鸡游戏

TA贡献1829条经验 获得超7个赞

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


我将其命名为 download.php


下载.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>

希望这可以帮助。


查看完整回答
反对 回复 2022-11-12
  • 2 回答
  • 0 关注
  • 121 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信