2

I am using following script to download a file via PHP:

header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename=sale.csv');
    header("Content-Transfer-Encoding: binary");
    header('Expires: 0');
    header('Pragma: no-cache');
    header("Content-Length: ".filesize($CSVFileName));
        readfile($CSVFileName);

$CSVFilename value is "/home/demo/public_html/testwordpress/csv/sale.csv" Also tried header("Content-type: text/csv");

Somehow it is not downloading the csv file given on the path. It is downloading source code of the page in csv. Here is the code i am getting:

<!DOCTYPE html>
<!--[if IE 6]>
<html id="ie6" lang="en-US">
<![endif]-->
<!--[if IE 7]>
<html id="ie7" lang="en-US">
<![endif]-->
<!--[if IE 8]>
<html id="ie8" lang="en-US">
<![endif]-->
<!--[if !(IE 6) | !(IE 7) | !(IE 8)  ]><!-->
<html lang="en-US">
<!--<![endif]-->
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<title>Call Details | Test</title>
<link rel="profile" href="http://gmpg.org/xfn/11" />

Please advise

Durga Dutt
  • 4,093
  • 11
  • 33
  • 48
  • 1
    Where is $CSVFileName specified? – PDev Sep 06 '13 at 15:34
  • 2
    Can you show us that what the downloaded file looks like? – Halcyon Sep 06 '13 at 15:34
  • what happens if you change the content type header to `header("Content-type: text/csv");` – Liam Allan Sep 06 '13 at 15:35
  • 1
    your content-type shouldn't have quotes around it. it's `Content-type: application/octet-stream`. – Marc B Sep 06 '13 at 15:36
  • I'm having the same problem, trying to download a PDF. Instead of the pdf, I get the source code of `download.php` saved as the PDF name. Not sure how this happens. I'm afraid it is something on php.ini? Not sure – DrBeco Jun 04 '23 at 03:01
  • I've added an answer specific to the root of the problem you reported below. Also, other answers you may want to read here https://stackoverflow.com/questions/7263923/how-to-force-file-download-with-php as well. – DrBeco Jun 04 '23 at 16:54

3 Answers3

1

I may be a little off my rocker here, but there is a much simpler way ( I think! ) to simply download a file.

$file = "/home/demo/public_html/testwordpress/csv/sale.csv";
$contents = file_get_contents($file);

// Manipulate the contents however you wish.
$newFilePath = "somewhere/over/the/rainbow/sale.csv";
file_put_contents($newFilePath, $contents);
  • The file_get_contents will work with http streams as well.
Rottingham
  • 2,593
  • 1
  • 12
  • 14
0

I made it this way:

<?PHP
    $filename= "yourfilepath.csv";

    header("Content-Disposition: attachment; filename=\"$filename\"");
    header("Content-Type: application/vnd.ms-excel");

    echo "your content to the .csv file";
?>
Gonzalo Acosta
  • 135
  • 1
  • 13
  • Have you tryed using ONLY the headers I gave you? – Gonzalo Acosta Sep 06 '13 at 15:58
  • Well I revised and reedited the code for it to work just by doing copy-paste(it just needed the php tags). Please, note that you are not going to output an existing file with this method, you're creating and writting it as it is downloaded by the user. – Gonzalo Acosta Sep 06 '13 at 16:29
0

Your code should work correctly, and I was having the same problem.

The problem you report is that the dialog box for saving the downloaded file do open and you save a kind of file (mime type), but when you open it, you see parts of the PHP source code as well.

The problem is that you did not separate PHP work from PHP output.

A PHP file, as a rule of thumb, should do only work, or only output, but not both. (OFC all rules of thumb have exceptions).

That means, for your download.php page work, do not echo anything, create the headers() and call the readfile() in the end, and then exit(). Do nothing else.

Thanks for the author of this gist https://gist.github.com/adrian-enspired/9ed2542a695e2ebe30aa (read for more information on PHP file organization).

DrBeco
  • 11,237
  • 9
  • 59
  • 76