Download corrupt.Need help

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
vearns
Forum Newbie
Posts: 3
Joined: Tue Jan 22, 2008 4:28 am

Download corrupt.Need help

Post by vearns »

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.

Code: Select all

<?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*8));
        flush();
        ob_flush();
    };
 
    fclose($FP);
    exit;
}
// How to use it
DownloadFile("http://files.brothersoft.com/internet/download_managers/FlashGet_54668.exe");
?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Download corrupt.Need help

Post by Jonah Bron »

It may be that you need to edit the php configuration file (php.ini), to treat absolute paths as files.
vearns
Forum Newbie
Posts: 3
Joined: Tue Jan 22, 2008 4:28 am

Re: Download corrupt.Need help

Post by vearns »

what setting i need to change ?
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Re: Download corrupt.Need help

Post by Mordred »

You have a hardcoded FileSize variable, that's your problem.
The Content-length: header should be given the correct file size.
vearns
Forum Newbie
Posts: 3
Joined: Tue Jan 22, 2008 4:28 am

Re: Download corrupt.Need help

Post by vearns »

ok,
i fixed the code but it still corrupt.
can anyone tell me whats the problem and how to fix it ?

this is my code

Code: Select all

 
<?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 ) )
            {
                continue;
            }
            return ( integer )$m[1];
        }
        return 0;
    }
    $filename = basename( $file );
    $file_extension = strtolower( substr( strrchr( $filename, "." ), 1 ) );
    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";
    }
    header( "Cache-Control:" );
    header( "Cache-Control: public" );
    header( "Content-Type: ".$ctype );
    if ( strstr( $_SERVER['HTTP_USER_AGENT'], "MSIE" ) )
    {
        $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 );
    if ( isset( $_SERVER['HTTP_RANGE'] ) )
    {
        list( $a, $range ) = explode( "=", $_SERVER['HTTP_RANGE'] );
        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 ".$rang."{$size2}/{$size}" );
    }
    else
    {
        $size2 = $size - 1;
        header( "Content-Range: bytes 0-".$size2."/{$size}" );
        header( "Content-Length: ".$size );
    }
    $fp = fopen( $file."", "rb" );
    if ( isset( $range ) && is_int( $range ) )
    {
        fseek( $fp, $range );
    }
    while ( !feof( $fp ) )
    {
        set_time_limit( 0 );
        echo fread( $fp, 8192 );
        flush( );
        ob_flush( );
    }
    fclose( $fp );
    exit( );
}
 
if ( !empty( $_GET['file'] ) )
{
    $file = $_GET['file'];
}
else
{
    $file = "http://files.brothersoft.com/internet/download_managers/FlashGet_54668.exe";
}
resume( $file );
?>
 
 
Post Reply