Jump to content

content type for php header


raman

Recommended Posts

I wish to allow the download of a text file using a php script but when I give Content-type:text/plain in header , it puts in the html tags in the text file.Can someone tell how I can generate a pure text file.

$filefordownload=fas.fasta;

header("Expires: 0");

  header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

  header("Cache-Control: no-store, no-cache, must-revalidate");

  header("Cache-Control: post-check=0, pre-check=0", false);

  header("Pragma: no-cache");

  header("Content-type:text/plain;charset:UTF-8");

  header('Content-length: '.strlen($content));

  header('Content-disposition: attachment; filename='.basename($filefordownload));//

 

Link to comment
https://forums.phpfreaks.com/topic/130982-content-type-for-php-header/
Share on other sites

You can try a force download of the file.

 

<?php
ob_start();

$file = $_GET['file'];

if(file_exists($file)){
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile($file);
}else {
echo "File does not exist";
}

ob_end_flush();
?>

Both the problems still persist.I have tried everything.This is the code I have ,the file is not already existing,I am dynamically generating it from a mysql database depending on what the user agent might select from the checkboxes.There are two submit buttons in this form one with value $exc and the other with $fasta.


 

if(empty($exc))

  {

    $file='fastafile.fasta';$type="plain/text";

    ob_start();

    //  echo"<p>";

    foreach($_POST['pro'] as $kyu){

      $re=mysql_query("SELECT * FROM fasta WHERE SNo='$kyu'");

      while($row=mysql_fetch_array($re))

{

  echo"".$row['header']."";echo"\n";echo"".$row['Sequence']."";echo"\n";

}

    }

  }else{

  $type="application/vnd.ms-excel;charset:UTF-8";

  $file = 'ExcelFile.xls';

  // start buffring

  ob_start();

  // sample dynamically generated data

  echo '<table border="1"> ';

  echo '<tr><th>Category</th><th>Protein</th><th>Brief Description</th></tr>';

  foreach($_POST['pro'] as $kyu){

    $re=mysql_query("SELECT * FROM cryptovir WHERE SNo='$kyu'");

    while($row=mysql_fetch_array($re))

      {

echo"<tr>";

echo"<td>".$row['Category']."</td>";

echo"<td>".$row['Name']."</td>";

echo"<td>".$row['Brief_Description']."</td>";

echo "</tr>";

   

      }

  }

}

 

  mysql_close($conn);

 

  $content = ob_get_contents();

  ob_end_clean();

 

header("Expires: 0");

header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

header("Cache-Control: no-store, no-cache, must-revalidate");

header("Cache-Control: post-check=0, pre-check=0", false);

 

header("Pragma: no-cache");

header("Content-type:$type;charset:UTF-8");

header('Content-length: '.strlen($content));

header('Content-disposition: attachment; filename='.basename($file));//

 

  // output all contents

  echo $content;

 

  exit;

I hate to do "drive by" problem solving, but it's all I have time for - so I may be (and probalby am) WAY off on this answer - but here's how I store my database queries as text - I've never had any problem with extra html tags - maybe there's something here that jumps out at you as different - wish I had more time to do a little testing - but I've got too many of my own problems at the moment.  Hope it helps ($applicants is a result of a mysql query): 

 

for ($i = 0; $i < $fields; $i++) {

$header .= mysql_field_name($applicants, $i);

if ($i<($fields-1)) {

$header = $header."\t";

}

}

 

while($row = mysql_fetch_row($applicants)) {

$line = '';

foreach($row as $value) {                                           

if ((!isset($value)) OR ($value == "")) {

$value = "\t";

} else {

$value = trim($value);

$value = $value . "\t";

}

$line .= $value;

}

$data .= trim($line)."\n";

}

$data = str_replace("\r","",$data);

 

if ($data == "") {

$data = "\n(0) Records Found!\n";                       

}

 

 

$filename="instructor_export_$EventID.txt";

header("Content-type: application/octet-stream");

header("Content-Disposition: attachment; filename=$filename");

header("Pragma: no-cache");

header("Expires: 0");

print "$header\n$data";

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.