Jump to content

Download text file


garrisonian14

Recommended Posts

hi,

I want to download a text file using php, Can any body please tell me the code. i am using following

 

$file_path = "terms.txt";

$path_parts = pathinfo($file_path);

$fext = $path_parts['extension'];

$asfname = $path_parts['basename'];

set_time_limit(0);

 

//echo "path".$file_path;

if(!file_exists($file_path))

{

die("File does not exist!");

}

 

// file size in bytes

$fsize = filesize($file_path);

 

 

  $mtype = '';

  // mime type is not set, get from server settings

  if (function_exists('mime_content_type'))

  {

$mtype = mime_content_type($file_path);

  }

  else if (function_exists('finfo_file'))

  {

$finfo = finfo_open(FILEINFO_MIME); // return mime type

$mtype = finfo_file($finfo, $file_path);

finfo_close($finfo); 

  }

 

 

  if ($mtype == '')

  {

    if($fext == "pdf")

  {

$mtype = 'application/pdf';

  }

  else if($fext == "txt" || $fext == "htm" || $fext == "html")

  {

$mtype = 'text/html';

  }

else

  {

$mtype = "application/force-download";

  }

  }

 

// set headers

header("Pragma: public");

header("Expires: 0");

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

header("Cache-Control: public");

header("Content-Description: File Transfer");

header("Content-Type: $mtype");

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

header("Content-Transfer-Encoding: binary");

header("Content-Length: " . $fsize);

@readfile($file_path);

 

 

But instead of downloading the file i am specifying , i downloads source code of the page where this download code is written.

 

Kindly help me as soon as possible.

 

 

regards,

Ali.

Link to comment
https://forums.phpfreaks.com/topic/80105-download-text-file/
Share on other sites

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.