Help - Search - Member List - Calendar
Full Version: how to hide the url of a file for downl load?
WorkTheWeb Forums > Webmaster Resources > PHP Help
Support our Sponsors!
_andrea.l
I'd like to let user download a file but I'd like to hide the url of the
file.
I'd like to write something like that:

<a href="download_file.php?id=1123">download file</a>

download_file.php get the file and give it to user.
BUT...
.... how can I write download_file.php?
thank you in advance for your help,
Andrea.

Stefan Rybacki
_andrea.l wrote:
QUOTE
I'd like to let user download a file but I'd like to hide the url of the
file.
I'd like to write something like that:

<a href="download_file.php?id=1123">download file</a

download_file.php get the file and give it to user.
BUT...
... how can I write download_file.php?
thank you in advance for your help,
Andrea.



something similar to this:

(by the way: have a look at the header function of PHP)

download_file.php

//get the real filename
$file=realfilename();

header("Content-type: application/force-download");
header("Content-Transfer-Encoding: Binary");
header("Content-length: ".filesize($file));
header("Content-disposition: attachment;
filename="".basename($file).""");
readfile("$file");

Regards
Stefan

Hilarion
QUOTE
I'd like to let user download a file but I'd like to hide the url of the
file.
I'd like to write something like that:

<a href="download_file.php?id=1123">download file</a

download_file.php get the file and give it to user.
BUT...
... how can I write download_file.php?
thank you in advance for your help,



<?php
// download_file.php
$file_locations = array(
1 => 'my/location/file1.zip',
2 => 'my/dislocation/file2.doc',
1123 => 'some/important/file.dbf'
);

Header( 'Location: http://my.server.com/' . $file_locations[ IntVal( $_GET[ 'id' ] ) ] );
?>


You could also read the file contents and return it to client
(giving correct headers first) but this way you'll block download
resuming etc. The main flaw of the solution above is that the
user can get the file location and then use it bypassing
download_file.php.
A good idea would be to get file locations from some database
(not PHP array) and to store somewhere information about
download performed (for download stats).


Hilarion

Stefan Rybacki
Hilarion wrote:
QUOTE
I'd like to let user download a file but I'd like to hide the url of
the file.
I'd like to write something like that:

<a href="download_file.php?id=1123">download file</a

download_file.php get the file and give it to user.
BUT...
... how can I write download_file.php?
thank you in advance for your help,




<?php
// download_file.php
$file_locations = array(
1 => 'my/location/file1.zip',
2 => 'my/dislocation/file2.doc',
1123 => 'some/important/file.dbf'
);

Header( 'Location: http://my.server.com/' . $file_locations[ IntVal(
$_GET[ 'id' ] ) ] );
?


This won't help much, since the real url is passed to the client and
that is what the OP wanted to avoid.

Regards
Stefan


QUOTE

You could also read the file contents and return it to client
(giving correct headers first) but this way you'll block download
resuming etc. The main flaw of the solution above is that the
user can get the file location and then use it bypassing
download_file.php.
A good idea would be to get file locations from some database
(not PHP array) and to store somewhere information about
download performed (for download stats).


Hilarion


Hilarion
QUOTE
<?php
// download_file.php
$file_locations = array(
1 => 'my/location/file1.zip',
2 => 'my/dislocation/file2.doc',
1123 => 'some/important/file.dbf'
);

Header( 'Location: http://my.server.com/' . $file_locations[ IntVal(
$_GET[ 'id' ] ) ] );
?

This won't help much, since the real url is passed to the client and
that is what the OP wanted to avoid.

She didn't write she wants to hide it before client. She only
wanted to hide it before the user. Most client apps do not present
the URL for the downloaded file so the above solution is OK in most
cases.


Hilarion

Stefan Rybacki
Hilarion wrote:
QUOTE
<?php
// download_file.php
$file_locations = array(
1 => 'my/location/file1.zip',
2 => 'my/dislocation/file2.doc',
1123 => 'some/important/file.dbf'
);
Header( 'Location: http://my.server.com/' . $file_locations[
IntVal( > $_GET[ 'id' ] ) ] );
?


This won't help much, since the real url is passed to the client and
that is what the OP wanted to avoid.


She didn't write she wants to hide it before client. She only
wanted to hide it before the user. Most client apps do not present
the URL for the downloaded file so the above solution is OK in most
cases.


Well at least if you're using a download manager you'll get the URL.
Never mind, I let it up to her. But it is also possible to provide a
resume function by the download script.

Regards
Stefan

QUOTE

Hilarion


Hilarion
QUOTE
[...] it is also possible to provide a resume function by the download script.

I assumed that it's possible but I suppose it's quite hard to code.
If you know some PHP code that does it, then please post it here (i've been
looking for something like that for long time). If you know any way how
to use Apache abilities from PHP to do it, then it's even better.


Hilarion

Stefan Rybacki
Hilarion wrote:
QUOTE
[...] it is also possible to provide a resume function by the download
script.


I assumed that it's possible but I suppose it's quite hard to code.
If you know some PHP code that does it, then please post it here (i've been
looking for something like that for long time). If you know any way how
to use Apache abilities from PHP to do it, then it's even better.


Hilarion



Its all about headers:


For example sending this to a client:

HTTP/1.1 200 OK
Connection: close
Date: Tue, 19 Oct 2004 15:11:23 GMT
Accept-Ranges: bytes
Last-Modified: Sun, 26 Sep 2004 15:52:45 GMT
ETag: "47febb2cfd76c41:2062"
Cache-Control: private
Content-Type: application/x-zip-compressed
Content-Length: 2844011

let the client know that resuming is possible, and for example the
client sends the following back:

GET http://192.168.100.100/download.zip HTTP/1.0
Range: bytes=822603-
Unless-Modified-Since: Sun, 26 Sep 2004 15:52:45 GMT
If-Range: "47febb2cfd76c41:2062"

Then you send something like this:

HTTP/1.1 206 Partial Content
Content-Range: bytes/2844011
Accept-Ranges: bytes
Last-Modified: Sun, 26 Sep 2004 15:52:45 GMT
ETag: "47febb2cfd76c41:2062"
Cache-Control: private
Content-Type: application/x-zip-compressed
Content-Length: 2021408


As you mentioned its kinda wasteful to keep track of what is sent to the
script but it is not that difficult.


As well on the fpassthru function page in the php manual:

"
nexz2004 at yahoo dot com
21-May-2004 09:30
also it is possible to make your php script resume downloads, to do this
you need to check $_SERVER['HTTP_RANGE'] which may contain something
like this
"bytes=10-" - resume from position 10, and to end of file

when sending response it is also needed to send with headers
Accept-Ranges: bytes
Content-Length: {filesize}
Content-Range: bytes 10-{filesize-1}/{ffilesize}

hope its usefull
"


On this address you will find a complete example of such a code:

http://de2.php.net/fread


Regards
Stefan

Hilarion
Stefan Rybacki wrote:

QUOTE
[...] it is also possible to provide a resume function by the download
script.

I assumed that it's possible but I suppose it's quite hard to code.
If you know some PHP code that does it, then please post it here (i've been
looking for something like that for long time). If you know any way how
to use Apache abilities from PHP to do it, then it's even better.

Its all about headers:
[...]

I know that :)


QUOTE
On this address you will find a complete example of such a code:

Thank you for the link (I'm using PHP manual in CHM file so it lacks many
user comments, but it's faster and makes searching easier) and for reminding
HTTP header fields related to the problem.


Hilarion


PHP Help | Linux Help | Web Hosting | Reseller Hosting | SSL Hosting
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2006 Invision Power Services, Inc.