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

前端 未结 3 1163
不思量自难忘°
不思量自难忘° 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: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 {
    

提交回复
热议问题