Jump to content

Recommended Posts

I am working on a script that parses a text file, extracts some information, stores the extracted information in a database, and writes a csv file for downloading and reading into a separate program. A simple html form is used to allow the user to point to the text file on their local machine, then on submitting the form runs the code to read, parse and write the file, the name of the csv file is determined by the code and changes with each text file processed.

Everything is working at this point except that when I use the headers in php to create the download file the cvs that is downloaded to the local machine includes the written csv file and below that all of the html from the page beginning with the DOCTYPE line and continuing through the final </html> tag. The html comes after the csv data. The csv file that is written to the server (currently localhost) is correct, containing only the csv data.

I suspect that there is something missing or incorrect in the header statements in the script that is causing this, but being new to this and a definite amature at programming, I have been unable to determine the problem.  The portion of the script that downloads the file is:

[pre]//download the written file. 
header('Content-Type: application/csv');
header("Content-Disposition: attachment; filename=\"$filename\""); 
header("Content-Lenght:  filesize('$filename')");
readfile($filename);[/pre]
 
I am running this using a WAMP5 installation on a Windows XP machine.

Thanks for any help,

William


Link to comment
https://forums.phpfreaks.com/topic/35685-file-download-includes-html/
Share on other sites

  • 2 years later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.