Jump to content

Letting users download w/o showing the full path to the file


john010117

Recommended Posts

I have a quick question.

 

I don't want my users to know the full path to a file that they're downloading. I instead want to display them as "download.php?file=whatever.zip", and let users download it when they click that link. How would I go about doing that? I know a database is involved (to store the full path to a file), but how would I let them download it when they visit that link?

Link to comment
Share on other sites

not as tricky as it can seem. all it generally involves is download.php retrieving the file info, sending the necessary headers and presenting the file just like any other type. i'm a bit rusty with headers and things, but something like:

 

<?php
$upload_dir = $_SERVER['DOCUMENT_ROOT'] . '/uploads';
$filename = $upload_dir . '/myfile.zip';

// send the headers
header("Content-Disposition: attachment; filename=\"".basename($filename)."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));

// read the file contents to the output buffer
readfile($filename);
exit;
?>

 

will do the trick. in this example i've assumed that you keep your files in www.yoursite.com/uploads although i wouldnt recommend it, to prevent direct linking to the files themselves. the good thing about scripts like this is they can serve a file from wherever - ie, not necessarily within your web root:

 

- httpdocs/htdocs

  - index.php

  - download.php

  - css

  ...etc...

- uploads

  ... uploaded files here. CANNOT be accessed directly from browser - only the download script.

 

in the above case, you'd simply change the $upload_dir to suit, such as /var/www/uploads or wherever you keep the directory.

 

good luck

Link to comment
Share on other sites

The same could be accomplished using mod_rewrite. It's an Apache module that basically takes a requested URL, applies a regex pattern to it and transforms it server side into what it needs to be. So you could have:

 

http://www.mysite.com/scripts/download.php?file=/dl/apps/photoshop.exe

 

turn into:

 

http://www.mysite.com/downloads/photoshop

 

cool stuff eh

Link to comment
Share on other sites

The same could be accomplished using mod_rewrite. It's an Apache module that basically takes a requested URL, applies a regex pattern to it and transforms it server side into what it needs to be. So you could have:

 

http://www.mysite.com/scripts/download.php?file=/dl/apps/photoshop.exe

 

turn into:

 

http://www.mysite.com/downloads/photoshop

 

cool stuff eh

 

Won't this become a problem if you have a lot of files? You would have to edit your htaccess file for every file.

Link to comment
Share on other sites

  • 1 month later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.