Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 01-22-2006, 10:28 PM
Reven Reven is offline
 
Join Date: Aug 2005
Location: UK
Posts: 113
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Dynamic Downloads in PHP

I want to create a script which will allow people to download files, but through dynamic links. For example, instead of using a static link such as example.com/files/file1.zip, where hot-linking can occur, I would like to do something like example.com/files/filedownload.php?file=file1, in which I can do checks to make sure the person downloading is logged in to vBulletin and stuff.

I don't know the name of the process of generating dynamic downloads, so I can't find anything on php.net. All I can think of doing is changing the header to the mime type of the file, but I don't know then how to start the download without the user having to click on something.

I don't want to redirect to the static file location either, as this is a security risk.

Any help would be fab. Cheers.
Reply With Quote
  #2  
Old 01-23-2006, 01:21 AM
Hellcat Hellcat is offline
 
Join Date: May 2003
Location: Germany
Posts: 560
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I have a script that does almost what you described - except for the logged in to vB thing (it checks an own login).

If it would help you I could just send you that script so you can "walk" through it and see how it does what.
IMHO that's always the best way to get such questions answered.

But a short one anyways
If you want to send a file, rather then a HTML page, you could use this block of code BEFORE you output anything else:
Code:
header ( "Content-Type: application/octet-stream");  // or whatever mime-type is appropriate
header ( "Content-Length: ".$filesize);  // filesize in bytes!
header ( "Content-Disposition: attachment; filename=\"$filename\"");
readfile($filepath);
Reply With Quote
  #3  
Old 01-23-2006, 08:27 AM
Reven Reven is offline
 
Join Date: Aug 2005
Location: UK
Posts: 113
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Great, I'll give that a shot. Cheers.

I want to allow people to download .zips and stuff, not HTML pages. I can do all of the vBulletin checks and stuff, I just didn't know how to send files like that.

Thanks again. If I have problems using the script, I'll know where to look

EDIT: I have built a script using the above info, but when the file is downloaded it is always 0 bytes in length. This is the script:

PHP Code:
if ($_REQUEST['do'] == "download")
{
    
$file $_REQUEST['file'];
    
$filetype $_REQUEST['type'];
    
$filename $file "." $filetype;
    
$file_location getcwd() . "/band_files/" $filename;
    
$filesize filesize($file_location);

    
header ("Content-Type: audio/mpeg");
    
header ("Content-Length: ".$filesize);
    
header ("Content-Disposition: attachment; filename=\"$filename\"");
    
readfile($filepath);

It doesn't work properly when using file_get_contents() in place of readfile() either. Anyone know what's wrong?
Reply With Quote
  #4  
Old 01-23-2006, 01:29 PM
Hellcat Hellcat is offline
 
Join Date: May 2003
Location: Germany
Posts: 560
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You are putting the path of your file in $file_location but in the readfile() call you use $filepath - try using $file_location there as well....
Reply With Quote
  #5  
Old 01-23-2006, 02:56 PM
Reven Reven is offline
 
Join Date: Aug 2005
Location: UK
Posts: 113
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Haha, I'm an idiot. It worked.

Thanks very much

I'd give you reputation if I could, and if it actually had any effect.
Reply With Quote
Reply

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

Forum Jump


All times are GMT. The time now is 06:49 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04585 seconds
  • Memory Usage 2,198KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)bbcode_code
  • (1)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (5)post_thanks_box
  • (5)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (5)post_thanks_postbit_info
  • (5)postbit
  • (5)postbit_onlinestatus
  • (5)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete