views:

141

answers:

6

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  A: 

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.

Rin
The first example should work and is necessary so that the browser has a clue on what to call the file.
Jesse Weigert
+1  A: 

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%5Fhttp%5Fheader.asp

Kalmi
A: 

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)

krike
Now you should mark most helpful answer... unless you don't need help anymore...
Rin
A: 

still a small bug :(

Everything works fine in firefox and safari but nog google chrome and IE the problem is that it won't add the extention or won't download properly in chrome and IE

I have the following header("Content-type: application/zip");

how should I fix that so it would download properly in all the browsers.

krike
A: 

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.

Jesse Weigert
A: 

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

ZJR