Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with download script

Status
Not open for further replies.

james0816

Programmer
Jan 9, 2003
295
US
I am using the script below to download files. However, when I open the file i just downloaded regardless of the format, it has the webpage appended to the top. what gives?

$filename = $row['name'];
$filename = realpath($filename);

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

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 "tif": $ctype="image/tif"; break;
case "png": $ctype="image/png"; break;
case "jpe": case "jpeg":
case "jpg": $ctype="image/jpg"; break;
default: $ctype="application/force-download";
}

if (!file_exists($filename)) {
die("NO FILE HERE");
}

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".@filesize($filename));
set_time_limit(0);
@readfile("$filename") or die("File not found.");
header('Refresh: 5; URL=bpflibrary.php');
exit();
 
remember that you cannot send headers after sending any content.

delete the line
Code:
header ("Refresh ...")
and all should work.
 
now it is a cache problem (unless the file you are trying to download is an html file). close the browser down and reopen it. if you still get the problem, explicitly flush the browser cache.

also remove the @ from readfile in case you are getting errors in your script.
 
still no luck....modified code as follows:

if (!file_exists($filename)) {
die("NO FILE HERE");
}

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: " .$ctype);
header("Content-Disposition: attachment; filename=\"".basename($filename)."\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
set_time_limit(0);
readfile($filename) or die("File not found.");
exit();
 
wow....i throughly confused. I just tried another script and got the same results. how can it append the HTML of the download page to the document?
 
that won't work because you are not providing a Content-Type.
 
sry...content type still from the first script in the SWITCH statement. that code has not changed so i did not repost it.
 
it cannot append the download page unless the filename you are providing the script is the filename of the page itself.

let's try forcing the issue

Code:
$filename = "tmp_".uniqid("", true)".txt";
$fh= fopen($filename, "wb");
fwrite ($fh,"This is some test content");
fclose ($fh);

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment;  filename="'.basename($filename).';");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
set_time_limit(0);
readfile($filename) or die("File not found.");
exit();
 
there's an error somewhere ... blank page displaying.
 
found the error...left out the "." in the $filename statement.....anyway....samething...putting html code on the download.
 
please replace this line
Code:
header('Content-Disposition: attachment;  filename="'.basename($filename).';");
with this line
Code:
header('Content-Disposition: attachment;  
        filename="'.basename($filename).'";');
and this line
Code:
$filename = "tmp_".uniqid("", true)".txt";
with this line
Code:
$filename = uniqid("tmp_", true).".txt";

please also ensure that there is NOTHING ELSE in the script apart from the corrected code.

please post back the results


 
Sry...same thing....I have remed out everything in the brackets with the exception of this code:

$filename = uniqid("tmp_", true).".txt";
$fh= fopen($filename, "wb");
fwrite ($fh,"This is some test content");
fclose ($fh);

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="'.basename($filename).'";');
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
set_time_limit(0);
readfile($filename) or die("File not found.");
exit();
 
sorry, I do not know what to say. the above script works fine for me on all three development platforms i have close to hand.

for the avoidance of doubt, please put the code into a separate file and point your browser at that file. give it a different name so that there can be no cacheing issues. since there is no html being generated in the file it is near impossible that any html can find its way in.

 
ok...i moved off to a seperate file and got just the text created.
 
there you go. so the whole concept of the http download works fine. there is therefore something wrong in the rest of the code that you have written.

if you are unable to debug then please post the WHOLE of the relevant code here. If it is very long, i have an upload server at (ignore the csv reference).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top