The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP-General - Problem downloading a file
Discuss Problem downloading a file in the PHP Development forum on Dev Shed. Problem downloading a file PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 31st, 2013, 11:43 AM
|
|
Registered User
|
|
Join Date: Mar 2011
Posts: 10
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.
|

January 31st, 2013, 11:46 AM
|
 |
Likely to be eaten by a grue.
|
|
Join Date: Oct 2006
Location: Pennsylvania, USA
|
|
|
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.
|

January 31st, 2013, 11:59 AM
|
|
Registered User
|
|
Join Date: Mar 2011
Posts: 10
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>
|

January 31st, 2013, 12:12 PM
|
|
|
|
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.
|

January 31st, 2013, 12:27 PM
|
|
Registered User
|
|
Join Date: Mar 2011
Posts: 10
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?
|

January 31st, 2013, 12:35 PM
|
|
|
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.
|

January 31st, 2013, 12:47 PM
|
|
Registered User
|
|
Join Date: Mar 2011
Posts: 10
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.
|

January 31st, 2013, 01:07 PM
|
|
|
|
I understand but what is the error? That is why I added the die.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|