john010117 Posted May 19, 2007 Share Posted May 19, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/52083-letting-users-download-wo-showing-the-full-path-to-the-file/ Share on other sites More sharing options...
redbullmarky Posted May 19, 2007 Share Posted May 19, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/52083-letting-users-download-wo-showing-the-full-path-to-the-file/#findComment-256856 Share on other sites More sharing options...
johnrcornell Posted May 19, 2007 Share Posted May 19, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/52083-letting-users-download-wo-showing-the-full-path-to-the-file/#findComment-256977 Share on other sites More sharing options...
john010117 Posted May 20, 2007 Author Share Posted May 20, 2007 Right. Man, I learn something new everyday (in this case, readfile() ). Thanks to both of you. I'll post again if I have any questions. Quote Link to comment https://forums.phpfreaks.com/topic/52083-letting-users-download-wo-showing-the-full-path-to-the-file/#findComment-257299 Share on other sites More sharing options...
gabeg Posted May 27, 2007 Share Posted May 27, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/52083-letting-users-download-wo-showing-the-full-path-to-the-file/#findComment-262751 Share on other sites More sharing options...
roopurt18 Posted May 28, 2007 Share Posted May 28, 2007 No. RewriteRule downloads/.* /path/to/script/downloads.php?file=$1 Quote Link to comment https://forums.phpfreaks.com/topic/52083-letting-users-download-wo-showing-the-full-path-to-the-file/#findComment-262971 Share on other sites More sharing options...
gabeg Posted May 28, 2007 Share Posted May 28, 2007 No. RewriteRule downloads/.* /path/to/script/downloads.php?file=$1 good point, I wasn't thinking, thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/52083-letting-users-download-wo-showing-the-full-path-to-the-file/#findComment-263099 Share on other sites More sharing options...
Daniel0 Posted May 28, 2007 Share Posted May 28, 2007 You shouldn't get the file from a GET parameter. That way people could download all sorts of files like configuration files containing passwords and such. Quote Link to comment https://forums.phpfreaks.com/topic/52083-letting-users-download-wo-showing-the-full-path-to-the-file/#findComment-263137 Share on other sites More sharing options...
roopurt18 Posted July 19, 2007 Share Posted July 19, 2007 As long as download.php performs checking to determine if the user has access to download the file they've requested, there's no harm in using a GET parameter. Messy, but perfectly valid. Quote Link to comment https://forums.phpfreaks.com/topic/52083-letting-users-download-wo-showing-the-full-path-to-the-file/#findComment-302633 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.