Help with PHP download.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
fannypack
Forum Newbie
Posts: 1
Joined: Wed Jun 24, 2009 10:14 am

Help with PHP download.

Post by fannypack »

I'm using a download script for my simplemachines forum but the problem is it's using read_chunk and the file I am offering to my visits is around 900mb. I'm having trouble determining if the script is just failing, or my host has effectively negated my ability to offer files in such a way. The download stops about midway through the transfer (400mb or so) Here is the code:

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;
 
    }
 
     
 
     
 
?>
 
What I am trying to do is have this script simply handle the download by sending the link directly, with no chunking, so that my download has resume support, etc.

Thank you.
Last edited by Benjamin on Wed Jun 24, 2009 10:44 am, edited 1 time in total.
Reason: Changed code type from text to php.
Post Reply