|  | |  |
07-03-2004, 05:37 PM
|
#1 (permalink)
| | Java fanboy
Join Date: Aug 2003
Posts: 1,230
| Download portal Newbie question. . .
Here's what I want to do: I have a set of files I want to made available for download, but I want to prevent direct linking to them by forcing a query to go through a "gatekeeper", some php file that checks to make sure the request is valid and from my site. While I can do the checking no problem, I'm trying to figure out how to initiate the download without actually making the files available via HTTP (somehow hiding the URL would work, although I'd prefer to use a .htaccess to restrict access to only localhost). I could do this with JSP, but I'm not familar enough with PHP functions; is there some way to push out a file in PHP? |
| |
07-03-2004, 11:01 PM
|
#2 (permalink)
| | Moderator
Join Date: May 2002 Location: us.ca
Posts: 4,706
| place your files below the web root. then use this function to basically force feed the files with the passthru function. PHP Code: <? function download($file) { $filesize = filesize($file); header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1 header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); // HTTP/1.0 header("Cache-control: private"); header("Content-Disposition: attachment; filename=$filename"); header("Content-Type: application/octet-stream"); header("Content-Length: $filesize"); header("Content-Transfer-Encoding: binary");
$fh = fopen($file, "rb"); fpassthru($fh); } ?>
__________________ Mike
Last edited by sde; 01-02-2007 at 06:23 AM.
Reason: added redhead's changes
|
| |
07-04-2004, 05:24 AM
|
#3 (permalink)
| | Java fanboy
Join Date: Aug 2003
Posts: 1,230
| Cool, thanks. I'll test it out. |
| |
07-29-2004, 04:07 PM
|
#4 (permalink)
| | Registered User
Join Date: Jul 2004
Posts: 2
| Hi guys *bump*..
This is exactly the thing I was looking for! But strangely enough I get the file as text when I use this function... So all kinds of strange charactars and stuf..
Don't have a clue why, somebody?
Thx in advance |
| |
07-29-2004, 09:22 PM
|
#5 (permalink)
| | Super Moderator
Join Date: Jun 2002 Location: Denmark
Posts: 1,879
| A wild guess: PHP Code: //header("Content-Transfer-Encoding: binary");
maybe it should have been PHP Code: header("Content-Transfer-Encoding: binary");
From php.net: Quote:
The default translation mode depends on the SAPI and version of PHP that you are using, so you are encouraged to always specify the appropriate flag for portability reasons. You should use the 't' mode if you are working with plain-text files and you use \n to delimit your line endings in your script, but expect your files to be readable with applications such as notepad. You should use the 'b' in all other cases.
If you do not specify the 'b' flag when working with binary files, you may experience strange problems with your data, including broken image files and strange problems with \r\n characters.
For portability, it is strongly recommended that you always use the 'b' flag when opening files with fopen(). | So the opening of the file should be: PHP Code: $fh = fopen("$file", "rb");
|
| |
07-30-2004, 01:47 AM
|
#6 (permalink)
| | Registered User
Join Date: Jul 2004
Posts: 2
| Thank you! It works |
| | | Thread Tools | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | All times are GMT -8. The time now is 02:10 AM. |
Copyright © 2000-2008, Milano Interactive Web Hosting provided by Portal 360 Web Hosting |  | |