#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    12
    Rep Power
    0

    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.
  2. #2
  3. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,846
    Rep Power
    6351
    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.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    12
    Rep 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>
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,332
    Rep Power
    594
    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.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    12
    Rep 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?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,332
    Rep Power
    594
    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) || 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.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2011
    Posts
    12
    Rep 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.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Jul 2003
    Posts
    3,332
    Rep Power
    594
    I understand but what is the error? That is why I added the die.
    There are 10 kinds of people in the world. Those that understand binary and those that don't.

IMN logo majestic logo threadwatch logo seochat tools logo