Download resume and zip file issue – PHP

I’m really stuck with it. I made a function as below (a wp plugin). That handles download requests.

The download url format is like this:

Read More

http://mysite.com/?download=2f547re8w9qasd547g8tr52e15469879w

Files are going to be saved as usual (with that pop up “save as…” box) in browser.

The problem is that:

1 – a download manager like Orbit tries to download a page! and IDM just downloads a low percentage of whole file size (e.g. 7%) then stops & by resuming, it begins downloading from first. (browser downloads files completely and resumes without problem)

2 – when a .zip file is downloaded (by browser), has an “Unexpected end of archive” error. (CRC32 in zip file details shows 0 chars)

function save_file()
{
    global $wpdb;
    global $uid;
    global $each_download;
    $hash = @$_GET["download"];
    if(preg_match('/^[a-z0-9]{32}$/i',$hash))
    {
        if($row = $wpdb->get_row("SELECT * FROM wp_dlurl WHERE hash = '{$hash}'",ARRAY_A))
        {
            if(is_user_logged_in())
            {
                if($row['price'] != 0)
                    $each_download = $row['price'];
                if(get_user_meta($uid, 'revo_credits', true) >= $each_download)
                {
                    $parts = pathinfo($row['url']);
                    $url = $parts['dirname'] . '/' . urlencode($parts['basename']);
                    $file = pathinfo($row['filename']);
                    $ext = $file['extension'];
                    /* List of File Types */ 
                    $fileTypes['swf'] = 'application/x-shockwave-flash'; 
                    $fileTypes['pdf'] = 'application/pdf'; 
                    $fileTypes['exe'] = 'application/octet-stream'; 
                    $fileTypes['zip'] = 'application/zip'; 
                    $fileTypes['doc'] = 'application/msword'; 
                    $fileTypes['xls'] = 'application/vnd.ms-excel'; 
                    $fileTypes['ppt'] = 'application/vnd.ms-powerpoint'; 
                    $fileTypes['gif'] = 'image/gif'; 
                    $fileTypes['png'] = 'image/png'; 
                    $fileTypes['jpeg'] = 'image/jpg'; 
                    $fileTypes['jpg'] = 'image/jpg'; 
                    $fileTypes['rar'] = 'application/rar';     

                    $fileTypes['ra'] = 'audio/x-pn-realaudio'; 
                    $fileTypes['ram'] = 'audio/x-pn-realaudio'; 
                    $fileTypes['ogg'] = 'audio/x-pn-realaudio'; 

                    $fileTypes['wav'] = 'video/x-msvideo'; 
                    $fileTypes['wmv'] = 'video/x-msvideo'; 
                    $fileTypes['avi'] = 'video/x-msvideo'; 
                    $fileTypes['asf'] = 'video/x-msvideo'; 
                    $fileTypes['divx'] = 'video/x-msvideo'; 

                    $fileTypes['mp3'] = 'audio/mpeg'; 
                    $fileTypes['mp4'] = 'audio/mpeg'; 
                    $fileTypes['mpeg'] = 'video/mpeg'; 
                    $fileTypes['mpg'] = 'video/mpeg'; 
                    $fileTypes['mpe'] = 'video/mpeg'; 
                    $fileTypes['mov'] = 'video/quicktime'; 
                    $fileTypes['swf'] = 'video/quicktime'; 
                    $fileTypes['3gp'] = 'video/quicktime'; 
                    $fileTypes['m4a'] = 'video/quicktime'; 
                    $fileTypes['aac'] = 'video/quicktime'; 
                    $fileTypes['m3u'] = 'video/quicktime';

                    $contentType = $fileTypes[$ext];
                    //ob_end_clean();
                    header("Cache-Control: public");
                    header('Content-Type: $contentType');
                    header("Content-Transfer-Encoding: binary");
                    $new_name = $row['filename'];//rand(1000,999999).".".$ext;
                    $contentDisposition = 'attachment';
                    if(strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
                    {
                        $new_name = preg_replace('/./', '%2e', $new_name, substr_count($new_name,'.') - 1);
                    }
                    $new_name = urlencode($new_name);
                    header("Content-Disposition: $contentDisposition; filename="$new_name"");
                    header("Accept-Ranges: bytes");
                    $range = 0; 
                    $size = $row['size']; 
                    if(isset($_SERVER['HTTP_RANGE']))
                    {
                        list($a, $range) = explode("=",$_SERVER['HTTP_RANGE']); 
                        str_replace($range, "-", $range); 
                        $size2 = $size-1; 
                        $new_length = $size-$range; 
                        header("HTTP/1.1 206 Partial Content"); 
                        header("Content-Length: $new_length"); 
                        header("Content-Range: bytes $range$size2/$size"); 
                    }
                    else
                    {
                        update_user_meta($uid, 'revo_credits', get_user_meta($uid, 'revo_credits', true)-$each_download);
                        $size2 = $size-1; 
                        header("Content-Range: bytes 0-$size2/$size"); 
                        header("Content-Length: ".$size);
                    }
                    if ($size == 0)
                    {
                        showMessage('aborted. zero file size');
                    } 
                    set_magic_quotes_runtime(0);
                    $maxSpeed = 200;            
                    $fp = fopen($url,"rb");
                    fseek($fp,$range);
                    while(!feof($fp) and (connection_status()==0)) 
                    { 
                        set_time_limit(0); 
                        print(fread($fp,1024*$maxSpeed)); 
                        flush(); 
                        ob_flush(); 
                        sleep(1); 
                    } 
                    fclose($fp); 
                    return((connection_status()==0) and !connection_aborted());
                }
                else
                    showMessage("not enough credit.");
            }
            else
                showMessage("login to download.");
        }
        else
        {
            wp_redirect(get_bloginfo('url'));
            exit;
        }
    }
    else
    {
        wp_redirect(get_bloginfo('url'));
        exit;
    }
}

I think headers are the culprits!


Resuming method with Accept-ranges and Content-length headers are already done and works when file is downloading by browser.

Related posts

Leave a Reply

1 comment

  1. Am not sure how other download works but your range implementation is wrong why ?

    You are getting range like this

    $_SERVER['HTTP_RANGE'] = "bytes=1-200"; //sample range
    $size = 1000; //sample size
    
    list($a, $range) = explode("=", $_SERVER['HTTP_RANGE']);
    str_replace($range, "-", $range);
    $size2 = $size - 1;
    $new_length = $size - $range;
    
    // Used Print Insted of headers
    print("HTTP/1.1 206 Partial Contentn");
    print("Content-Length: $new_lengthn");
    print("Content-Range: bytes $range$size2/$sizen");
    

    Output

    HTTP/1.1 206 Partial Content
    Content-Length: 999
    Content-Range: bytes 1-200999/1000   <------ This is an issue 
    

    I expect something like this

    list($a, $range) = explode("=", $_SERVER['HTTP_RANGE']);
    list($offset, $length) = explode("-", $range);
    $length = $length - $offset;
    
    // Used Print Insted of headers
    print("HTTP/1.1 206 Partial Contentn");
    print("Content-Length: $lengthn");
    printf('Content-Range: bytes %d-%d/%d', $offset, ($offset + $length), $size)
    

    Output

    HTTP/1.1 206 Partial Content
    Content-Length: 199
    Content-Range: bytes 1-200/1000   <---- Properly Displayed