Jump to content

Recommended Posts

Hello,

i dont know what is the problem with this coding.

when i try to download file from other server,the file can be download but it is corrupt.

if i use DAP,the download will stuck at 100%.

if i use flashget,the download is complete but the file is corrupt.

 

however,if i download a file from local server,everything went fine.

 

this is my code.

 

hope anyone can help me.

 

thanks.

 

<?php
function DownloadFile($File)
{
    
    $FileName = basename($File);
    $FileExt = strtolower(substr(strrchr($FileName,"."),1));
    
    switch($FileExt)
        {
        case "exe":
                        $ctype = "application/octet-stream";
                        break;
        case "zip":
                        $ctype = "application/zip";
                        break;
        case "mp3":
                        $ctype = "audio/mpeg";
                        break;
        case "mpg":
                        $ctype = "video/mpeg";
                        break;
        case "avi":
                        $ctype = "video/x-msvideo";
                        break;
                case "wmv":
                        $ctype = "video/x-ms-wmv";
                        break;
        default:
                        $ctype="application/force-download";
    }
    
    header("Cache-Control:");
    header("Cache-Control: public");
    
    header("Content-Type: $ctype");
    if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
        {
        // IE Bug workaround
        $IEFileName = preg_replace('/\./', '%2e', $FileName, substr_count($FileName, '.') - 1);
        header("Content-Disposition: attachment; filename=\"$IEFileName\"");
    }
        else
        {
        header("Content-Disposition: attachment; filename=\"$FileName\"");
    }
    header("Accept-Ranges: bytes");
    
    $FileSize = 4653240;
    // 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);
        $FileSize2 = $FileSize-1;
        $new_length = $FileSize2-$Range;
        header("HTTP/1.1 206 Partial Content");
        header("Content-Length: $new_length");
        header("Content-Range: bytes $Range$FileSize2/$FileSize");
    }
        else
        {
        $FileSize2 = $FileSize-1;
        header("Content-Range: bytes 0-$FileSize2/$FileSize");
        header("Content-Length: ".$FileSize);
    }
    // Open the file
    $FP = fopen("$File","rb");
    // Seek to start of missing part
    fseek($FP,$Range);
    
    while(!feof($FP))
        {
        // Reset time limit so there's no timeout
        set_time_limit(0);
        print(fread($FP,1024*);
        flush();
        ob_flush();
    };

    fclose($FP);
    exit;
}
// How to use it
DownloadFile("http://files.brothersoft.com/internet/download_managers/FlashGet_54668.exe");
?>

Link to comment
https://forums.phpfreaks.com/topic/87201-download-corruptneed-help/
Share on other sites

i changed some of the code.

but again,it is not working...arghhhh!!

 

<?php
function resume($file){

function httpStreamSize($file) 
{ 
  $meta_data = stream_get_meta_data($file); 
  foreach($meta_data['wrapper_data'] as $response)  
    if (preg_match('#^Content-Length\s*:\s*(\d+)$#i', $response,$m))  
      return (int)$m[1]; 
  return null; 
}
    
    //Gather relevent info about file
    $len = filesize($file);
    $filename = basename($file);
    $file_extension = strtolower(substr(strrchr($filename,"."),1));
    
    //This will set the Content-Type to the appropriate setting for the file
    switch( $file_extension ) {
        case "exe": $ctype="application/octet-stream"; break;
        case "zip": $ctype="application/zip"; break;
        case "mp3": $ctype="audio/mpeg"; break;
        case "mpg":$ctype="video/mpeg"; break;
        case "avi": $ctype="video/x-msvideo"; break;
        default: $ctype="application/force-download";
    }
    
    //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', $filename, substr_count($filename, '.') - 1);
        header("Content-Disposition: attachment; filename=\"$iefilename\"");
    } else {
        header("Content-Disposition: attachment; filename=\"$filename\"");
    }
    header("Accept-Ranges: bytes");
    $fp=fopen("$file","rb");

    $size=httpStreamSize($fp);
fclose($fp);
    //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,1024*);
        flush();
        ob_flush();
    }
    fclose($fp);
    exit;
}

resume("http://files.brothersoft.com/internet/download_managers/FlashGet_54668.exe");
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.