Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Apr 2007
    Posts
    144
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Download fails on files greater than 10M

    Using the following code, files greater than 10Megs do not download. Less than 10 is working. Can anyone explain why? Files that will be downloaded are 5G plus in size.

    PHP Code:
        function download() {

            global 
    $_GET;

            
    //Gather relevent info about file
            
    $file "/user/dac420/tts/incoming/".$_GET['name'];
            
    $len filesize($file);
            
    $filename basename($file);
            
    $file_extension strtolower(substr(strrchr($filename,"."),1));

            
    // Determine correct MIME type
            
    switch($file_extension){

                case 
    "asf":     $ctype "video/x-ms-asf";                break;
                case 
    "avi":     $ctype "video/x-msvideo";               break;
                case 
    "exe":     $ctype "application/octet-stream";      break;
                case 
    "mov":     $ctype "video/quicktime";               break;
                case 
    "mp3":     $ctype "audio/mpeg";                    break;
                case 
    "mpg":     $ctype "video/mpeg";                    break;
                case 
    "mpeg":    $ctype "video/mpeg";                    break;
                case 
    "rar":     $ctype "encoding/x-compress";           break;
                case 
    "txt":     $ctype "text/plain";                    break;
                case 
    "wav":     $ctype "audio/wav";                     break;
                case 
    "wma":     $ctype "audio/x-ms-wma";                break;
                case 
    "wmv":     $ctype "video/x-ms-wmv";                break;
                case 
    "zip":     $ctype "application/x-zip-compressed";  break;
                default:        
    $ctype "application/force-download";    break;

            }

            
    //Begin writing headers
            
    header("Cache-Control:");
            
    header("Cache-Control: public");

            
    //Use the switch-generated Content-Type
            
    header("Content-Type: $ctype");

            if (
    strstr($_SERVER['HTTP_USER_AGENT'], "MSIE")) {
                
    # workaround for IE filename bug with multiple periods / multiple dots in filename
                # that adds square brackets to filename - eg. setup.abc.exe becomes setup[1].abc.exe
                
    $iefilename preg_replace('/\./''%2e'$filenamesubstr_count($filename'.') - 1);
                
    header("Content-Disposition: attachment; filename=\"$iefilename\"");
            } else {
                
    header("Content-Disposition: attachment; filename=\"$filename\"");
            }

            
    header("Accept-Ranges: bytes");

            
    $size=filesize($file);

            
    //check if http_range is sent by browser (or download manager)
            
    if(isset($_SERVER['HTTP_RANGE'])) {

                list(
    $a$range)=explode("=",$_SERVER['HTTP_RANGE']);

                
    //if yes, download missing part
                
    str_replace($range"-"$range);
                
    $size2=$size-1;
                
    $new_length=$size2-$range;

                
    header("HTTP/1.1 206 Partial Content");
                
    header("Content-Length: $new_length");
                
    header("Content-Range: bytes $range$size2/$size");

            } else {

                
    $size2=$size-1;
                
    header("Content-Range: bytes 0-$size2/$size");
                
    header("Content-Length: ".$size);

            }

            
    //open the file
            
    $fp=fopen("$file","rb");

            
    //seek to start of missing part
            
    fseek($fp,$range);

            
    //start buffered download
            
    while(!feof($fp)){

                
    //reset time limit for big files
                
    set_time_limit(0);

                print(
    fread($fp,filesize($file)));
                
    flush();
                
    ob_flush();

            }

            
    fclose($fp);
            exit;

        } 
    Last edited by Inigoesdr; 03-04-2008 at 09:22 PM.

  • #2
    Regular Coder
    Join Date
    Feb 2007
    Location
    Canada
    Posts
    924
    Thanks
    10
    Thanked 56 Times in 55 Posts
    do a phpinfo() to see what the max_file_downloads size is on the server?

  • #3
    Regular Coder
    Join Date
    Apr 2007
    Posts
    144
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Can you please tell me what the exact value title is. I think its one of these.

    upload_max_filesize 100000M
    post_max_size 100000M

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    What is the memory_limit set at?

    Please remember to read the stickies for this forum. In particular the one about using [php][/php] tags when posting code.

  • #5
    Regular Coder
    Join Date
    Apr 2007
    Posts
    144
    Thanks
    5
    Thanked 0 Times in 0 Posts
    The solution is to split the file into chunks when sending. Thanks to everyone for your suggestions.

    replace the while() in my above code.

    PHP Code:
        //start buffered download
        
    while(!feof($fp)){

            
    $total     filesize($file);
            
    $sent      0;
            
    $blocksize = (<< 20); //2M chunks
            
    $handle    fopen($file"r");

            
    // Now we need to loop through the file and echo out chunks of file data
            // Dumping the whole file fails at > 30M!
            
    while($sent $total){
                echo 
    fread($handle$blocksize);
                
    $sent += $blocksize;
            }

            exit(
    0);
            @
    flush();
            @
    ob_flush();
        } 

  • #6
    Regular Coder
    Join Date
    Apr 2007
    Posts
    144
    Thanks
    5
    Thanked 0 Times in 0 Posts
    So now that I have the download working I need to know why the page is not refreshing at the end of the script. The download happen, then nothing. How do i get the page to continue after download?

  • #7
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,647
    Thanks
    2
    Thanked 406 Times in 398 Posts
    I don't see any code that would forward the user there.. but there is an oddly placed exit statement.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •