download link hidden

萝らか妹 提交于 2019-12-06 15:07:12
IProblemFactory

This example should help you:

$len = filesize($filename);
header("Content-type: image/jpeg");
header("Content-Length: $len");
header("Content-Disposition: inline; filename=\"$new_filename\"");
readfile($filename);

or another one, looks simpler for me:

<?php
header('Content-type: image/jpeg');
$f = file_get_contents('path/to/image.jpg');
print $f;
?>

PS of course, you content-type must fit to your file.

header() must be called before any actual output(echo) is sent, otherwise it will throw this error.

See "Example 2": http://www.w3schools.com/php/func_http_header.asp

wooohoooo

thanks a lot guys, this site rules I always get the right answer :D

I pasted this above the header.php include ob_start( );

and just before the header functions ob_ get_clean();

might be usefull to people (note: for the second function I've added a space before the get so it would display correctly, don't forget to remove it when u use it)

If you're ever going to try and scale this service, I suggest taking a look at perlbal. One of the neat tricks that it does is that your app can send a special header which tells perlbal to serve a static file some another server. This way you don't need to tie up a PHP thread with pushing bits down to a client.

Content Disposition is ok, but another solution would be using PATH_INFO and get the file this way:

http://example.com/download.php/2793156879.zip

Your download.php will be like

// handle path_info
$filename=$_SERVER['PATH_INFO']; // gets "/2793156879.zip" as $filename

// do smtg w/ $filename...
// ...

// download 
$len = filesize($filename);
header("Content-type: application/force-download");
header("Content-Length: $len");
readfile($filename);

Note: application/force-download does not exists, it's just there to force downloads with every browser there is. (some MSIEs seem to have had troubles with application/octet-stream)

This approach has the advantage of working with all browsers: even the very old ones, not supporting "Content-Disposition:" header.

It has the small disadvantage you have to substr() the product code yourself from the PATH_INFO string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!