PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 31st, 2013, 11:43 AM
handro1104 handro1104 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2011
Posts: 10 handro1104 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 21 m 21 sec
Reputation Power: 0
PHP-General - Problem downloading a file

My code is:
Code:
		header('Content-type: text/plain');
		header('Content-Disposition: attachment; filename=test.txt');
		readfile($filePath);


The file exists and has one line, but I am getting:
<html>
<head></head>
<body>
This is a line</body>
</html>

Your help will be greatly appreciated.

Reply With Quote
  #2  
Old January 31st, 2013, 11:46 AM
ManiacDan's Avatar
ManiacDan ManiacDan is offline
Likely to be eaten by a grue.
Dev Shed God 10th Plane (9500 - 9999 posts)
 
Join Date: Oct 2006
Location: Pennsylvania, USA
Posts: 9,791 ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)ManiacDan User rank is General 77th Grade (Above 100000 Reputation Level)  Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1Folding Points: 127430 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 2 Months 3 Weeks 14 h 53 m 20 sec
Reputation Power: 6112
Look in your code for HTML tags. You've given us 3 lines, there's clearly more.
__________________
HEY! YOU! Read the New User Guide and Forum Rules

"They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

"The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

Reply With Quote
  #3  
Old January 31st, 2013, 11:59 AM
handro1104 handro1104 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2011
Posts: 10 handro1104 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 21 m 21 sec
Reputation Power: 0
My entire code

Code:
<html>
<head></head>
<body>
	<?php
	if (!isset($_GET['submit'])) {
		?>
	<h1>Download File</h1>
	<form action="http://127.0.0.1/Test/Download.php" method="get">
		<p>
			<input type="submit" name="submit" value="Download" />
		</p>
	</form>
	<?php
	} else {
		//  Create a file to upload.
		$filePath = getcwd() . "\\test.txt";
		header('Content-type: text/plain');
		header('Content-Disposition: attachment; filename=test.txt');
		readfile($filePath);
	}
	?>
</body>
</html>

Reply With Quote
  #4  
Old January 31st, 2013, 12:12 PM
gw1500se gw1500se is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,867 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 1 Week 5 Days 9 h 6 m 6 sec
Reputation Power: 581
The headers have to be first. All the HTML stuff in front causes standard headers to be output so your headers will do nothing. There can be absolutely nothing output before the headers are output including whitespace.
__________________
There are 10 kinds of people in the world. Those that understand binary and those that don't.

Reply With Quote
  #5  
Old January 31st, 2013, 12:27 PM
handro1104 handro1104 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2011
Posts: 10 handro1104 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 21 m 21 sec
Reputation Power: 0
Still having problems

Thank you very much for your help; I have only three month with PHP. I created a separate file for down load:
Code:
<?php
//  Create a file to download.
$filePath = "C:\\handro\\test.txt";
$handle = fopen( $filePath, “w” );
fwrite( $handle, 'Single line again');
fclose( $handle );
header('Content-type: text/plain');
header('Content-Disposition: attachment; filename=test.txt');
readfile($filePath);


However I am getting an even larger HTML.

I did notice that $handle is false although I can edit the file. I am running/debugging in Eclipse PHP under wamp. Is there anything that I have to set up?

Reply With Quote
  #6  
Old January 31st, 2013, 12:35 PM
gw1500se gw1500se is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,867 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 1 Week 5 Days 9 h 6 m 6 sec
Reputation Power: 581
That means it was not able to open the file in write mode for whatever reason. You need to add some error checking.
PHP Code:
 $handle fopen$filePath“w” ) || die("Open error on $filePath: ".error_get_last()); 

Also note the use of [ PHP ] tags rather than [ CODE ] tags. See the sticky at the top of this forum.

Reply With Quote
  #7  
Old January 31st, 2013, 12:47 PM
handro1104 handro1104 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2011
Posts: 10 handro1104 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 21 m 21 sec
Reputation Power: 0
I get the error but ...

The problem is that I am getting a false value. I have been reading that Windows has a problem with fopen, but I haven't found any solutions.

Reply With Quote
  #8  
Old January 31st, 2013, 01:07 PM
gw1500se gw1500se is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jul 2003
Posts: 2,867 gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level)gw1500se User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Year 1 Week 5 Days 9 h 6 m 6 sec
Reputation Power: 581
I understand but what is the error? That is why I added the die.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-General - Problem downloading a file

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap