Having a small problem, and I'm not sure how to fix this. I'm writing a file to the server using fwrite, and than once it's written, I want an open/save dialog to popup so that the user can download the file to their computer. The problem is this: the file that you save in the open/save dialog box has the contents of the file PLUS the html contents of the PHP within it. Obviously not ideal.

Here's my code:

$filename = "csv_files/" . date("YmdHis") . ".txt";

$handle = fopen($filename, 'w+');
fwrite($handle, $txt);
fclose($handle);

@header("Content-Type: application/octet-stream"); //sets type
@header("Content-Disposition: attachment; filename=$filename"); //gets filename
@header("Content-Description: Download File"); // describes what you are doing
@readfile($filename);

I've already checked -- the contents of the files on the server are correctly being written with only the correct content. It's the readfile() that is point of the problem. Any help would be greatly appreciated.

    Write a Reply...