Downloading file in php

How to download file in php,plz check my code

Page 1 of 1

4 Replies - 1619 Views - Last Post: 03 January 2010 - 12:48 PM Rate Topic: -----

#1 rajesh.kaushals  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-January 10

Downloading file in php

Posted 03 January 2010 - 03:25 AM


<?php
	 if(isset($_GET['down']) AND $_GET['down'] == "ok")
	 {
	  header('Content-Type:application/txt\n');
	  header("Content-Length:2000\n");
	  header('Content-Disposition: inline; filename="http://localhost/abc.txt"');
	  readfile("http://localhost/abc.txt");
	 }
?>

<a href="http://localhost/download.php?down=ok">download this file</a>





Is This A Good Question/Topic? 0
  • +

Replies To: Downloading file in php

#2 no2pencil  Icon User is online

  • Admiral Fancy Pants
  • member icon

Reputation: 5388
  • View blog
  • Posts: 27,384
  • Joined: 10-May 07

Re: Downloading file in php

Posted 03 January 2010 - 03:39 AM

You can only send the header once.

Why are you having php handle the download rather than just supplying an href?
Was This Post Helpful? 0
  • +
  • -

#3 cam888  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 64
  • Joined: 20-February 09

Re: Downloading file in php

Posted 03 January 2010 - 04:23 AM

Just use file_get_contents() to get the file from the server, as long as it is defined in your php.ini that you can do that.
Was This Post Helpful? 0
  • +
  • -

#4 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 339
  • View blog
  • Posts: 1,185
  • Joined: 02-April 09

Re: Downloading file in php

Posted 03 January 2010 - 04:38 AM

if(isset($_GET['down'])
{
	JamesBrown = true;
}


Was This Post Helpful? 0
  • +
  • -

#5 mraldo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 03-January 10

Re: Downloading file in php

Posted 03 January 2010 - 12:48 PM

<?php
	 if(isset($_GET['down']) && $_GET['down'] == "ok")
	 {
		header("Content-Type:application/txt");
		header("Content-Length:2000");
		header('Content-Disposition: attachment; filename="abc.txt"');
		readfile("abc.txt");
		exit;
	 }
?>

<a href="http://localhost/download.php?down=ok">download this file</a>


Try that out, assuming that this download file is in the same directory as the abc.txt file. Also note that you don't need to add line breaks in headers, and that you can not do carriage returns (the \r\n) in single quotes, in PHP single quotes means a literal string, so when you output that string, it looks just like it does in the source, and also, when using readfile, you shouldn't use a URL to the file, but a path to the file.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1