亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用 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 關注
  • 168 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號