Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 07-03-2004, 05:37 PM   #1 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
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?
__________________
GitS
Belisarius is offline   Reply With Quote
Old 07-03-2004, 11:01 PM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,487
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 07-04-2004, 05:24 AM   #3 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,161
Belisarius is on a distinguished road
Cool, thanks. I'll test it out.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 07-29-2004, 04:07 PM   #4 (permalink)
MarkOV
Registered User
 
Join Date: Jul 2004
Posts: 2
MarkOV is on a distinguished road
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
MarkOV is offline   Reply With Quote
Old 07-29-2004, 09:22 PM   #5 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,710
redhead is on a distinguished road
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"); 
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is online now   Reply With Quote
Old 07-30-2004, 01:47 AM   #6 (permalink)
MarkOV
Registered User
 
Join Date: Jul 2004
Posts: 2
MarkOV is on a distinguished road
Thank you! It works
MarkOV is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
steam a csv file for download. sde Java 1 07-27-2004 03:15 PM
is there a free download MySQL GUI window? Ploy PHP 3 07-09-2004 04:39 PM
PHP Portal Systems (PHPNuke, PostNUKE, etc...) Admin PHP 1 07-07-2004 04:47 AM
File DownLoad in java Gopinath_java Java 1 06-19-2004 02:45 AM
Streaming Media Download Ilya020 Lounge 3 01-16-2004 06:25 PM


All times are GMT -8. The time now is 05:07 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting