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 :)

2
  • Make sure to remove any prior echo statements or blank lines before <?php which might cause the header header('Content-Type: application/octet-stream'); to not really be written. Commented Feb 10, 2015 at 22:44
  • your a saint, it works now :) didn't realise that it was needed for this! Commented Feb 10, 2015 at 22:55

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.