Welcome to Dream.In.Code
Become a PHP Expert!

Join 137,429 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,893 people online right now. Registration is fast and FREE... Join Now!




Download corrupt

 
Reply to this topicStart new topic

Download corrupt

vearns
22 Jan, 2008 - 01:37 AM
Post #1

D.I.C Head
**

Joined: 5 Dec, 2006
Posts: 51


My Contributions
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
<?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");
?>


This post has been edited by vearns: 22 Jan, 2008 - 01:38 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/5/08 04:53AM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month