Code: Select all
<?php
/**********************************************\
| SMFSHOP (Shop MOD for Simple Machines Forum) |
| (c) 2005 DanSoft Australia |
| |
\**********************************************/
//File: filedownload2.php
// File Download Item
//VERSION: 1.2 (Build 5) --Fixed v1
//DATE: 15th April 2006
class item_filedownload2 extends itemTemplate {
function getItemDetails() {
$this->name = "Download xxx File v2";
$this->desc = "Download a file [INSERT FILE DESCRIPTION HERE]";
$this->price = 200;
$this->require_input = false;
$this->can_use_item = true;
}
//see AddToPostCount for more info
function getAddInput() {
return "File Name: <input type='text' name='info1' value='file_name_here.txt' size='60'><br>
This file name does NOT need to be the same name as the file on the server. This name
is what the file will be saved as on the user's computer.<br><br>
File Path (INCLUDING FILE NAME): <input type='text' name='info2' value='../../file_name_here.txt' size='60'><br>
<b>IMPORTANT: </b>Make SURE that this file path is <b>OUT of your webroot.</b> If your forum is
stored at /home/myuser/public_html/forum/ then store the downloadable file at
/home/myuser/files/ or similar. Otherwise, you run the risk of the file being
downloaded without being paid for.<br>
<input type='checkbox' name='info3'>Delete item from inventory after use";
}
function onUse() {
global $item_info, $db_prefix;
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename={$item_info[1]}");
readfile_chunked($item_info[2], false);
if (isset($item_info[3]) && $item_info[3] = "on") {
$result = db_query("DELETE FROM {$db_prefix}shop_inventory
WHERE id = {$_GET['id']}",
__FILE__, __LINE__);
}
exit();
}
}
function readfile_chunked($filename,$retbytes=true) {
$chunksize = 1*(1024*1024); // how many bytes per chunk
$buffer = '';
$cnt =0;
// $handle = fopen($filename, 'rb');
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, $chunksize);
echo $buffer;
ob_flush();
flush();
if ($retbytes) {
$cnt += strlen($buffer);
}
}
$status = fclose($handle);
if ($retbytes && $status) {
return $cnt; // return num. bytes delivered like readfile() does.
}
return $status;
}
?>
Thank you.