How to download a script in php

I want to make a download link on my page, the user uploads a text file to the server, I want to upload these files, plz help me, I'm new to php,

-1
source share
2 answers

You need to set the headers correctly , then print the contents of the file .

+3
source

You need to get the mime type and file size, and then set the correct headers before sending the file. Try the following:

function sendFile($file) {
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    header('Content-type: ' . finfo_file($finfo, $file));
    header('Content-length: ' . filesize($file));
    $filename = basename($file);
    header("Content-Disposition: attachment; filename='$filename'");
    fpassthru($file);
    finfo_close($finfo);
}
+3
source

All Articles