How to download a file on clicking the name of file using PHP?

前端 未结 3 1103
不思量自难忘°
不思量自难忘° 2020-12-02 01:02

I Have a list of information in which there is a field where name of file is hyperlinked. I want the user to download that particular file when he clicks on it.

so,

相关标签:
3条回答
  • 2020-12-02 01:14

    so, How to download a file on clicking the name of file using PHP?

    The easiest way should be linking to it.

    <a href="download.php?......" target="_blank">DMS.doc</a>
    
    0 讨论(0)
  • 2020-12-02 01:19

    You don't need AJAX for that, you cannot even do what you want using AJAX. If you want to obfuscate URL to a file, then replace your download function with this:

    function download(path,val) {
        window.location.href = path+"download.php?val="+val;
    };
    
    0 讨论(0)
  • 2020-12-02 01:27
         // Fetch the file info.
    
    
    $filePath = $_SERVER['DOCUMENT_ROOT'] . "/dfms/images/docs/".$filename;
    
    
    
    if(file_exists($filePath)) {
        $fileName = basename($filePath);
        $fileSize = filesize($filePath);
    
        // Output headers.
        header("Cache-Control: private");
        header("Content-Type: application/stream");
        header("Content-Length: ".$fileSize);
        header("Content-Disposition: attachment; filename=".$fileName);
    
        // Output file.
        readfile ($filePath);                   
        exit();
    }
    else {
    
    0 讨论(0)
提交回复
热议问题