-2

I would like to download a file from a folder on my server and automatically start the download without the person seeing the original file link.

Here is my current script, but it downloads an invalid corrupted file.

$filename = "dsk.zip";
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename="./zz/'.$filename.'");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize("./zz/".$filename));
ob_end_flush();
@readfile("./zz/".$filename);

Can you please help me? The file, dsk.zip, is in the folder zz.

user3783243
  • 5,368
  • 5
  • 22
  • 41
  • There are syntax errors in this, like `filename="./zz/'.$filename.'"`. Fix those first. – ceejayoz Sep 02 '21 at 18:16
  • 3
    `filename="./zz/'.$filename.'"` You cannot define a folder on the user PC, in fact the file name is only a hint and can be ignored by the user in the dialog they will see. SO use a simple `filename=".$filename.'"` – RiggsFolly Sep 02 '21 at 18:16
  • Does this answer your question? [Create a zip file and download it](https://stackoverflow.com/questions/12225964/create-a-zip-file-and-download-it) – Ruben Sep 02 '21 at 18:16
  • The best is to change your **server** folder name or file name but is tricky. somedomain.com/FOLDER_NAME_/dsk.zip **OR** somedomain.com/download/dsk-20210902-15464.zip – Patfreeze Sep 02 '21 at 18:29
  • What happens with this code? Do you receive an error, if you remove error suppression? – user3783243 Sep 02 '21 at 18:58
  • Can you please share the error? – Rajeev Singh Sep 06 '21 at 12:24
  • Please provide enough code so others can better understand or reproduce the problem. – Community Sep 06 '21 at 12:25
  • Fix the quotes on this line: `header("Content-Disposition: attachment; filename="./zz/'.$filename.'");` – Luuk Sep 11 '21 at 10:43

1 Answers1

0
$file = "dsk.zip"; // this is what you will get from a param (i.e. ?file=dsk.zip) or from DB query

header("Cache-Control: public");

header("Content-Description: File Transfer");

header("Content-Disposition: attachment; filename='zz/'.$file"); 

header("Content-Length: ".filesize($file));

header("Content-Type: application/force-download");

header("Content-Transfer-Encoding: binary");

readfile('zz/'.$file);
user3783243
  • 5,368
  • 5
  • 22
  • 41