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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to force download in PHP

Status
Not open for further replies.

misslois74

Programmer
Sep 27, 2008
63
0
0
PH
im currently working on a site wherein on the backend the administrator would be able to force download the file that has been attached by a user in the front end i cant seem to figure it out since whats happening right now is the browser is loading the file, special characters are appearing on the page....

here is my sample code:

Code:
if($op=='download') {
    list($fpath,$filename,$file)=sqlget("
        select filepath,filename,file from employer_sent_files
        where file_id='$id'");
    
	$file = $_GET['filename'];

	header('Cache-Control: private');
	header('Pragma: private');
	header("Content-Transfer-Encoding: binary") ; 
	header("Content-Disposition: attachment; filename=\"" . basename($file) . "\"");
	header("Content-Length:" . filesize($file));
	header("Content-Type: application/application/octet-stream;");
	readfile($file);
}

the part which calls this is the ff:

echo "<tr>
<td class=Arial11Grey><input type='checkbox' name='id2[]' value='$id'><a href=$PHP_SELF?op=download&id=$id&filename=$fname><u>$fname</u></a></td>
<!--<td class=Arial11Grey><input type='checkbox' name='id2[]' value='$id'><a href=$PHP_SELF?op=download&id=$id><u>$fname</u></a></td>-->
<td class=Arial11Grey>".nice_size($size)."</td>
<!--<td class=Arial11Grey>".str_replace(',','<br>',$tags)."</td>-->
<td class=Arial11Grey>$type</td>
<td class=Arial11Grey>$added</td>
<td class=Arial11Grey>$employer</td>";


thanks in advance
 
thanks for the quick reply feherke, i already remove the extra application but still its not working the problem the file is loading within the browser right now im testing on an excel file so its displaying dummy characters on the page my goal is to prompt a dialog box to ask the user if he/she wants to download the file...
 
Hi

By the way, which browser ? ( Explorer used to be "clever" and ignore the [tt]Content-type[/tt] sent by the server. )

I have no idea. Could we see that page in action ?


Feherke.
 
ok this is what i notice when i upload a word document file this are the things displaying in the page:

?????????ÿÿ?????????????????ˆ????? ?????? ?? ?????? ?????? ?????? ?????? ?????????????´??????¸??????¸??????¸??????¸??,???ä??<???´??????§???j??,??????,??????,??????,??????,?????? ?????? ?????? ??????&??????(???????(???????(???????(???????(???????(???$???A??R??cC??\???L?????????????????????? ?????? ?????????????????????? ?????? ?????? ?????? ??????L??????????????? ?????? ??????,??????????????,??Û???a??????À ??????À ??????À ?????? ??R??? ??????,?????? ??????,??????&???????????????À ?????????????????????????????????????????????????????? ??????&???????????????À ??????À ??Â??Š8??ü?? ?????? ??????????????????????????????????????????????????????????????=??????,?????? ?? ???PSc-t‡É????????¸??????Z ?????†:??&???????????b>??Ä???w???0???§???????¬:??f??¿C??????p ??:???¿C??L???=??????????????´??????´?????? ?????? ?????? ?????? ??????????????=?????¿C?????????????? ??????&=??<?? ??L???S ??6???À ??????‰ ??,???µ ??S?????????????????????????????????? ?????? ?????? ??????L???????L???????´??????´????¸??????????????ª ?????´??????´??????¸????????????????????????????????????????????
????????????????????????????????????????????????????????????????????????

but if its a text file its displaying just fine.. by the way im using Mozilla Firefox...

and i've reduced my script with just the ff.

Code:
if($op=='download') {
    list($fpath,$filename,$file)=sqlget("
        select filepath,filename,file from employer_sent_files
        where file_id='$id'");
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"$filename\"");
    echo $file;
    exit();

but i guess this script just display the content of the file within the page what i would like to do is to force download the file....
 
Hi

I understand what you want and I understand what you get.

I can imagine two things :
[ul]
[li]the [tt]$op=='download'[/tt] evaluates to [tt]false[/tt][/li]
[li]there was output before calling the [tt]header()[/tt] functions[/li]
[li]a stupid proxy or security software alters the HTTP headers[/li]
[li]you have a browser extension installed which alters the browser behavior[/li]
[/ul]

We would need to see the page in action. If that is not possible, please check the HTTP headers sent together with the document. See if the [tt]Content-type[/tt] is the one the code you posted sends. Use a tool for that :
[ul]
[li]Browser:
[ul]
[li]FireFox:
[ul]
[li]Live HTTP Headers[/li]
[li]FireBug[/li]
[li]Web Developer[/li]
[/ul]
[/li]
[li]Lynx[/li]
[li]Links[/li]
[li]W3M[/li]
[li]Explorer:
[ul]
[li]ieHTTPHeaders[/li]
[li]IEWatch[/li]
[/ul]
[/li]
[/ul]
[/li]
[li]tool:
[ul]
[li][tt]netcat[/tt][/li]
[li]PuTTY[/li]
[li][tt]telnet[/tt][/li]
[li][tt]Wget[/tt][/li]
[li][tt]cURL[/tt][/li]
[/ul]
[/li]
[/ul]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top