Forum Moderators: coopster

Message Too Old, No Replies

Force Download

         

Shae

10:05 pm on Feb 17, 2006 (gmt 0)

10+ Year Member



I am new to php and I am really confuse. I am trying to follow the information in:

[webmasterworld.com...]

But I am lost all I get are header errors... I am not sure if it is the old php that we run with or most likely me...

I was hoping someone would help me out

my download php :

<?php

function download($file)
{
$filename = $file;
print($filename .' is the file name \n' );
$filename = realpath($filename); //server specific

$file_extension = strtolower(substr(strrchr($filename,"."),1));

if (! file_exists( $filename ) )
{
die("NO FILE HERE");
};
switch( $file_extension )
{
case "pdf": $ctype="application/pdf"; break;
case "exe": $ctype="application/octet-stream"; break;
case "zip": $ctype="application/zip"; break;
case "doc": $ctype="application/msword"; break;
case "xls": $ctype="application/vnd.ms-excel"; break;
case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
case "gif": $ctype="image/gif"; break;
case "png": $ctype="image/png"; break;
case "jpe": case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}
header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=".basename($filename).";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".@filesize($filename));
@readfile("$filename") or die("File not found.");
exit();
}
?>

my test.php as I call it is:

<?php
include('download.php'); // this is your script which I've turned into a function.
?>

<?php
if (isset($_POST['download'])) {
$filename =('leaveform.pdf');//$_POST['leaveform.pdf'];
download($filename);
}
?>
<body>
<form method="post" action="<?php echo $_SERVER['download.php'];?>">
<input type="submit" name="download" value="Click to Download the PDF"/>
<input type="hidden" name="file" value="leaveform.pdf" />
</form>

<? print($filename . " is the file name.")?>
</body>

can any tell me where I am going wrong?

Birdman

10:30 pm on Feb 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to WebmasterWorld, Shae!

It will be easier to pinpoint the problem if you show us the error message. I'm sure someone will get you fixed right up.

Shae

10:39 pm on Feb 17, 2006 (gmt 0)

10+ Year Member



Ok here goes... these are cut from the webpage

errors:

leaveform.pdf is the file name \n
Warning: Cannot add header information - headers already sent by (output started at /var/www/html/Test1.php:7) in /var/www/html/download.php on line 29

Warning: Cannot add header information - headers already sent by (output started at /var/www/html/Test1.php:7) in /var/www/html/download.php on line 30

Warning: Cannot add header information - headers already sent by (output started at /var/www/html/Test1.php:7) in /var/www/html/download.php on line 31

Warning: Cannot add header information - headers already sent by (output started at /var/www/html/Test1.php:7) in /var/www/html/download.php on line 32

Warning: Cannot add header information - headers already sent by (output started at /var/www/html/Test1.php:7) in /var/www/html/download.php on line 33

Warning: Cannot add header information - headers already sent by (output started at /var/www/html/Test1.php:7) in /var/www/html/download.php on line 34

Warning: Cannot add header information - headers already sent by (output started at /var/www/html/Test1.php:7) in /var/www/html/download.php on line 35

Warning: Cannot add header information - headers already sent by (output started at /var/www/html/Test1.php:7) in /var/www/html/download.php on line 36
%PDF-1.4 %âãÏÓ 8 0 obj << /Linearized 1 /O 11 /H [ 4985 640 ] /L 68697 /E 65904 /N 1 /T 68420 >> endobj xref 8 198 0000000016 00000 n 0000004306 00000 n 0000004413 00000 n 0000005625 00000 n 0000005963 00000 n 0000006403 00000 n 0000006687 00000 n 0000007113 00000 n 0000007531 00000 n 0000007643 00000 n 0000008992 00000 n 0000009217 00000 n 0000009441 00000 n 0000009667 00000 n 0000009891 00000 n 0000010116 00000

The Binary garbage is, I beleive, the file.

Birdman

1:41 pm on Feb 18, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks to me like output is being sent to the browser too early. Even though it's just a newline, it still counts.

<?php
include('download.php'); // this is your script which I've turned into a function.
?>
RIGHT HERE
<?php
if (isset($_POST['download'])) {
$filename =('leaveform.pdf');//$_POST['leaveform.pdf'];
download($filename);
}
?>

Try this instead:

<?php
include('download.php');

if (isset($_POST['download'])) {
$filename =('leaveform.pdf');//$_POST['leaveform.pdf'];
download($filename);
}
?>

Shae

11:58 pm on Feb 18, 2006 (gmt 0)

10+ Year Member



No it still gives me the same errors.

jatar_k

12:04 am on Feb 19, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you need to look for other places where something is being output.

does eith of those files hav a blank line outside of the <?php?> tags, any hard return will do it.

is there any other output you can see being sent to the browser?

Shae

2:03 am on Feb 19, 2006 (gmt 0)

10+ Year Member



Yes there is more out put and I will put that below. If I get rid of the function and just go to download.php it downloads just fine. I also have taken out all of the print commands and It still gives the sam header errors.

The out put in its entierty is to large to post. These are the last few lines.

0000000016 00000 n 0000004306 00000 n 0000004413 00000 n 0000005625 00000 n 0000005963 00000 n 0000006403 00000 n 0000080689 00000 n 0000080711 00000 n 0000081102 00000 n 0000081124 00000 n 0000081522 00000 n 0000081544 00000 n 0000081937 00000 n 0000081959 00000 n 0000082362 00000 n 0000082384 00000 n 0000082775 00000 n 0000082797 00000 n 0000083196 00000 n 0000083218 00000 n 0000083609 00000 n 0000083631 00000 n 0000084029 00000 n 0000084051 00000 n trailer << /Size 264 /Info 7 0 R /Root 9 0 R /Prev 173 /ID[] >> startxref 86015 %%EOF

also the code for the Test1.php is :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<?php
include('download.php');

if (isset($_POST['download']))
{
$filename =('leaveform.pdf');//$_POST['leaveform.pdf'];
download($filename);
}
?>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="submit" name="download" value="Click to Download the PDF"/>
<input type="hidden" name="file" value="leaveform.pdf" />
</form>

</body>
</html>

Birdman

2:05 pm on Feb 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to move the PHP code above the HTML. Once you output any character, it's too late to send headers. I've rearranged it below for you. I also made another small change, moving the include() inside the if{} statement. No need to include it if you don't need it.

<?php
if (isset($_POST['download']))
{
include('download.php');
$filename =('leaveform.pdf');//$_POST['leaveform.pdf'];
download($filename);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<?php
include('download.php');

if (isset($_POST['download']))
{
$filename =('leaveform.pdf');//$_POST['leaveform.pdf'];
download($filename);
}
?>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input type="submit" name="download" value="Click to Download the PDF"/>
<input type="hidden" name="file" value="leaveform.pdf" />
</form>

</body>
</html>

Shae

12:23 am on Feb 20, 2006 (gmt 0)

10+ Year Member



After moving it it still gives header errors this time they are:

Warning: Cannot add header information - headers already sent by (output started at /var/www/html/download.php:37) in /var/www/html/download.php on line 28

I am not sure how to fix this and I am getting very frustrated. I am thinking that what I want to do is impossible.

THanks

AlexK

12:48 am on Feb 20, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Have you gotten hold of the main thing that has been said?

- headers() cannot be sent if output has already been made

(remembering that 'output' can be

echo()
statements, embedded HTML or even "white space" outside of the
<php? ...?>
statements)

If so, then what you need to investigate is:

Shae

12:53 am on Feb 20, 2006 (gmt 0)

10+ Year Member



After rooting around on the internet, it turns out that there was a setting in the php.ini file that needed to be changed. The output_buffering = was set to off and I changed it to 4096 and restarted the web and it works great. THanks for your help .