2

I am storing files above the root directory of my server, and I'm planning on giving users a download by using a php file.

Here is my code: For the download link:

<a href="'.FILEGRAB_ADR.'?adr='.$file.'">Download</a>

and for the FileGrab.php file:

<?php
//This will grab a file from the server

if (file_exists(UPLOAD_ADR.$_GET['adr'])) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="example.CATPart"');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header("Content-length: ".filesize(UPLOAD_ADR.$_GET['adr'])); 

   readfile(UPLOAD_ADR.$_GET['adr']);
   exit;
}
?>

I got the above code (in essence) from here

FileGrab.php is loading but it looks like it is just spewing out a raw text form of the file, arbitrary text/symbols etc.

Checked that the file exists, and file size returns a value. Don't know how to get anymore errors to check out of it!

Does it make a difference this is a .CATPart file, which is a little abstract but necessary...

Any help greatly appreciated :)

Jamie Robinson
  • 832
  • 10
  • 16

1 Answers1

0

Try to use buffering functions by surrounding your code between ob_start() and ob_end_flush (); . plus of course make sure that no spaces or echos are used before sending the headers .

For more info check these links:

http://php.net/manual/en/ref.outcontrol.php

Why use output buffering in PHP?

Community
  • 1
  • 1