【问题标题】:With PHP how to force download of file with randon file name and type使用 PHP 如何强制下载具有随机文件名和类型的文件
【发布时间】:2020-03-24 01:13:44
【问题描述】:

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

我在网络服务器上有一个文件夹,其中可以包含几种不同的文件类型,文件名是随机的。 允许的文件类型是: 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可以轻松实现吗?

【问题讨论】:

    标签: php download dialog


    【解决方案1】:

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

    我称之为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>
    

    希望这会有所帮助。

    【讨论】:

    • 太棒了。非常感谢。一切似乎都运作良好。修改了用法(以适合我的情况),以: echo '
    • '.$read.'
    • ';
    【解决方案2】:
    <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');
        }
    
    ?>
    

    【讨论】:

      猜你喜欢
      相关资源
      最近更新 更多
      热门标签