Items:Download File v2

From SMFShopWiki

Jump to: navigation, search

drondron Hello!I don't see why my qositeun has not published here yet. Any way, I ask my qositeun again. With the help of this site, I was able to center my post titles, but the problem is that they are not clickable any more; or oddly partially clickable.What is wrong with it?ThanksMinoudad

The 'Download File v2' item is a modified version of the 'Download File' item included with SMFShop. It allows files larger than 10 MB to be used. Basically, the item that comes with SMFShop reads the whole file into memory, which doesn't work for large files. This item instead reads the file in 1 MB chunks, which is more efficient for larger files.

This file should be saved as filedownload2.php.

  1. <?php
  2. /**********************************************\
  3. | SMFSHOP (Shop MOD for Simple Machines Forum) |
  4. | (c) 2005 DanSoft Australia |
  5. | http://www.dansoftaus.uni.cc/ |
  6. \**********************************************/
  7.  
  8. //File: filedownload2.php
  9. // File Download Item
  10.  
  11. //VERSION: 1.2 (Build 5) --Fixed v1
  12. //DATE: 15th April 2006
  13.  
  14. class item_filedownload2 extends itemTemplate {
  15. function getItemDetails() {
  16.  
  17. $this->name = "Download xxx File v2";
  18. $this->desc = "Download a file [INSERT FILE DESCRIPTION HERE]";
  19. $this->price = 200;
  20. $this->require_input = false;
  21. $this->can_use_item = true;
  22. }
  23. //see AddToPostCount for more info
  24. function getAddInput() {
  25. return "File Name: <input type='text' name='info1' value='file_name_here.txt' size='60'><br>
  26. This file name does NOT need to be the same name as the file on the server. This name
  27. is what the file will be saved as on the user's computer.<br><br>
  28. File Path (INCLUDING FILE NAME): <input type='text' name='info2' value='../../file_name_here.txt' size='60'><br>
  29. <b>IMPORTANT: </b>Make SURE that this file path is <b>OUT of your webroot.</b> If your forum is
  30. stored at /home/myuser/public_html/forum/ then store the downloadable file at
  31. /home/myuser/files/ or similar. Otherwise, you run the risk of the file being
  32. downloaded without being paid for.<br>
  33. <input type='checkbox' name='info3'>Delete item from inventory after use";
  34. }
  35.  
  36. function onUse() {
  37. global $item_info, $db_prefix;
  38. header("Content-type: application/octet-stream");
  39. header("Content-Disposition: attachment; filename={$item_info[1]}");
  40. readfile_chunked($item_info[2], false);
  41. if (isset($item_info[3]) && $item_info[3] = "on") {
  42. $result = db_query("DELETE FROM {$db_prefix}shop_inventory
  43. WHERE id = {$_GET['id']}",
  44. __FILE__, __LINE__);
  45. }
  46. exit();
  47.  
  48. }
  49. }
  50.  
  51. function readfile_chunked($filename,$retbytes=true) {
  52. $chunksize = 1*(1024*1024); // how many bytes per chunk
  53. $buffer = '';
  54. $cnt =0;
  55. // $handle = fopen($filename, 'rb');
  56. $handle = fopen($filename, 'rb');
  57. if ($handle === false) {
  58. return false;
  59. }
  60. while (!feof($handle)) {
  61. $buffer = fread($handle, $chunksize);
  62. echo $buffer;
  63. flush();
  64. if ($retbytes) {
  65. $cnt += strlen($buffer);
  66. }
  67. }
  68. $status = fclose($handle);
  69. if ($retbytes && $status) {
  70. return $cnt; // return num. bytes delivered like readfile() does.
  71. }
  72. return $status;
  73.  
  74. }
  75.  
  76.  
  77. ?>
Personal tools