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
Share on other sites

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.