Welcome Guest, Not a member yet? Register   Sign In
Using Download Helper With Extremely Large Files
#1

[eluser]dawnerd[/eluser]
Hi everyone. Lets cut to the point.

I have a little over 12 hours before my client wants her website done. I have already built a custom user management, cart, and pretty much everything. I wanted to make the videos users could buy only downloadable to people who are logged in and have purchased it (the video is 63 minutes flv, comes to about 400 megs.

I just realized that this way of doing it is flawed as the max script memory in php fills up very fast and crashes the script. I have an upper cap of 350 megs, setting it higher is not an option.

I was wondering if there is another way to do this that would be easy to implement in the time I have left.

Any help will be greatly appreciated.
#2

[eluser]Tom Schlick[/eluser]
why not just provide them with a direct link to the file but using a random string in the url to find the path.

such as mysite.com/download/djfoidfh3pt99fh
and that would link them to

mysite.com/files/file12345.flv

all you would need to do is include that random string in the database feild for each flash video and your golden
#3

[eluser]dawnerd[/eluser]
That's actually how I'm doing it now. Here's a snippet from my download controller:
Code:
function paid()
    {
        //Check if user is logged in
        if(!$this->users->isLoggedIn())
        {
            echo "Not allowed.";
            return false;
        }
        
        $requested_file = $this->uri->segment(3);
        
        $query = $this->db->getwhere('downloads', array('id'=>$requested_file));
        foreach ($query->result() as $row)
        {
            
            $file_data =  read_file($this->rootdir."paid/".$row->name.".".$row->filetype);
            $file_name = $row->name.".".$row->filetype;
        }
        force_download($file_name, $file_data);
    }

The problem comes up when CI loads the file as a string to force the download. I really don't want the file to be within the public's reach. Right now it sits under the html folder.
#4

[eluser]nirbhab[/eluser]
Rather than reading the file you can redirect the user to file path, it won't fail your script. let me also search for the solution before your client kills you :-)
#5

[eluser]dawnerd[/eluser]
If I redirect them to a file that's below the public html folder, then they won't be able to view it. Users can download multiple files, but the download script does not handle that. The foreach loop was me being lazy/tired. I haven't slept in three days.
#6

[eluser]nirbhab[/eluser]
Sorry i didn't got you...

Quote:If anything is in the public html directory, according to me everybody can view or download the files.

Ur script has to actually redirect to the correct path. that's it....but are you thinking of something like rapidshare?

sleep well, it will giv u more energy for thinking, n rest is must, sometimes it help in getting new idea or solving problems...
#7

[eluser]nirbhab[/eluser]
got some links or hint below

Code:
header('Content-Length: 1234');
found common in both, hope it might help u out.

Quote:http://elouai.com/force-download.php
http://www.jonasjohn.de/snippets/php/headers.htm

please somebody help him.
#8

[eluser]dawnerd[/eluser]
In a way it has to act like rapidshare. I guess I could use the readfile function but that still will use up script memory, unless it streams the file... you know, I think you links may have helped. I'm going to test this right now and see if I have any luck.
#9

[eluser]nirbhab[/eluser]
best of luck, headers might help u out.
#10

[eluser]dawnerd[/eluser]
I did some reason on the readfile function and I found that through some sly programming, you can mimic the readfile function, but have it read the file in chucks, thus avoiding the max memory errors.

Here's the code that was posted on php.net

Code:
<?php
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;

}
?>

I'm sure I'm not the only one that could use this. Now, to get it to work in code igniter...




Theme © iAndrew 2016 - Forum software by © MyBB