PHP Forums Archive

php file download and annoying html tags

Tags: download, html, header, output

dcoder posted on 2011-01-09 01:49:31 #

Hello,

The code below works on my localhost for downloading files. However, on top of my downloaded file there are 160 lines of HTML tags. How can I eliminate these tags and just get the plain file? I am using wordpress plug-in to download files.
I appreciate your advice.

$fd = basename($csv);
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"".$fd."\"");
header("Content-Description: Download");
readfile($csv);

Comments / discussions

Olaf posted on 2011-01-09 09:05:36 #

Hello dcoder,
is this the only code you use for the file download or are there other (wordpress) files included?

What kind of HTML code do you get?

dcoder posted on 2011-01-14 19:40:48 #

Hi Olaf, thanks for your help! Yes that is the only code I use. here is a sample html code on top of my file. I think they are the html header. I am just giving you some of the html junks on top of my original file. The file I am trying to download is a simple text file. I have added this function to clear the header, which it does. However, when I try to download the text file, the download dialog does not pop up and my file is displayed in the browser.

function flush1(){
echo str_repeat('',256)."

";

    if (ob_get_length()){
        ob_flush();
        flush();
        ob_end_flush();
    }
    ob_start();
	}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<head profile="http://gmpg.org/xfn/11">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Get File | Winifred Street</title>
<link rel="stylesheet" rel="nofollow" href="http://localhost/wordpress/wp-content/themes/Winifred_Street/style.css" type="text/css" media="screen" />
<!--[if IE 6]><link rel="stylesheet" rel="nofollow" href="http://localhost/wordpress/wp-content/themes/Winifred_Street/style.ie6.css" type="text/css" media="screen" /><![endif]-->
<!--[if IE 7]><link rel="stylesheet" rel="nofollow" href="http://localhost/wordpress/wp-content/themes/Winifred_Street/style.ie7.css" type="text/css" media="screen" /><![endif]-->
<link rel="pingback" rel="nofollow" href="http://localhost/wordpress/xmlrpc.php" />
<link rel="alternate" type="application/rss+xml" title="» Feed" rel="nofollow" href="http://localhost/wordpress/feed/" />
<link rel="alternate" type="application/rss+xml" title="» Comments Feed" rel="nofollow" href="http://localhost/wordpress/comments/feed/" />
<link rel='stylesheet' id='contact-form-7-css'

dcoder posted on 2011-01-14 22:17:33 #

Here is my latest code:

function flush1(){
echo str_repeat('',256)."

";

    if (ob_get_length()) {
        @ob_flush();
        @flush();
        @ob_end_flush();
    }
    @ob_start();
	}

function send_file() {
	flush1(); 	

	$csv = DPATH . "/file.csv";	

	@header('Content-Type: text/plain');
    @header('Content-Disposition: attachment; filename="'.basename($csv).'"');
	echo file_get_contents($csv);
	exit;
	}

dcoder posted on 2011-01-15 03:03:05 #

I have added the @ symbol to the header function to suppress the message "Warning: Cannot modify header information - headers already sent". There is not any html or white spaces in my code. I trigger the above code using the shortcode from a wordpress page. Once I view the page the code is executed. I hope I have given you enough information.

Olaf posted on 2011-01-15 07:46:37 #

Hi,
the problem is that your script sends data to the browser before all headers are send. You need to avoid this, otherwise you will not exceed. Start your script simple, check the download script on from this site and see what's happen. NEVER use the @ sign!

dcoder posted on 2011-01-15 15:22:30 #

Hi,
download script is where I started to do my script. I got the same message "Warning: Cannot modify header information - headers already sent" with that script. How can I avoid sending the header first in wordpress?

dcoder posted on 2011-01-15 15:39:12 #

The message "Warning: Cannot modify header information - headers already sent" points to the lines:
header('Content-Type: text/plain');
header('Content-Disposition: attachment; filename="'.basename($csv).'"');

Olaf posted on 2011-01-15 18:37:27 #

what is exact the code you're using? remove every whitespace (outside the php tags)