问题
I'm working on a shop system and I have the following link http://cmstutorials.org/shop/downloads/2793156879 (original link is cmstutorials. org/shop/downloads.php?download=2793156879)
what I'm trying to do is let the user download the item that matches with the id 2793156879 withouth showing the actual link to the file. Like they have on themeforest.net
how would I do this?
回答1:
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.
回答2:
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
回答3:
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)
回答4:
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.
回答5:
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
来源:https://stackoverflow.com/questions/1514171/download-link-hidden