PHP Downloading-script

Discussion in 'Movies & Media' started by Master of Disaster, Jul 15, 2007.

Thread Status:
Not open for further replies.
  1. Master of Disaster Merlin's Housekeeper

    Joined:
    May 20, 2007
    Location:
    Twilight Town
    1
    27
    Hello everyone,

    I don't know where to put this, but perhabs somebody can help me. I wrote this Downloadingscript and it does work, when the urls are like this:

    HTML:
    <a href="download.php?location=images/test.jpg">link</a>
    but not like this
    HTML:
    <a href="download.php?location=http://www.kh-legacy.net/images/test.jpg">link</a>
    I need the second version because some downloads are at for example imagehack and photobucket. What I have to change so that the second link-example does work, too? Here's the script:

    PHP:
    <?
    $_GET['location'] = $location;

    function 
    download_data($file,$download_speed 256)
    {
        if(
    file_exists($file)) /* Existiert Datei Ã¼berhaupt? */
        
    {
            if(
    is_readable($file)) /* Kann sie auch gelesen werden? */
            
    {
            
    $size_of_file filesize($file); /* Größe der Datei */

            
    $extension_of_file strtolower(substr(strrchr($file,"."),0)); /* Datei-Endung filtern */

            
    $name_of_file str_replace(" ","%20",basename($file)); /* Leerzeichen ersetzen */

            
    header("HTTP/1.1 200 OK");

            
    header("Expires: 0"); /* KEIN cachen */
            
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            
    header("Cache-Control: private",FALSE);
            
    header("Pragma: public");
            
    header("Content-Description: File Transfer"); /* Download eben xD */
            
    header("Content-Transfer-Encoding: binary"); /* Uebertragungs-Art */

                
    switch(TRUE)
                {
                    case(
    $extension_of_file == ".exe"):
                    {
                    
    $content_type "application/octet-stream";
                    break;
                    }

                    case(
    $extension_of_file == ".zip"):
                    {
                    
    $content_type "application/zip";
                    break;
                    }

                    case(
    $extension_of_file == ".rar"):
                    {
                    
    $content_type "application/rar";
                    break;
                    }

                    case(
    $extension_of_file == ".doc"):
                    {
                    
    $content_type "application/msword";
                    break;
                    }

                    case(
    $extension_of_file == ".xls"):
                    {
                    
    $content_type "application/vnd.ms-excel";
                    break;
                    }

                    case(
    $extension_of_file == ".ppt"):
                    {
                    
    $content_type "application/vnd.ms-powerpoint";
                    break;
                    }

                    case(
    $extension_of_file == ".gif"):
                    {
                    
    $content_type "image/gif";
                    break;
                    }

                    case(
    $extension_of_file == ".png"):
                    {
                    
    $content_type "image/png";
                    break;
                    }

                    case(
    $extension_of_file == ".jpg" or $extension_of_file == ".jpeg"):
                    {
                    
    $content_type "image/jpg";
                    break;
                    }

                    case(
    $extension_of_file == ".mp3"):
                    {
                    
    $content_type "audio/mpeg";
                    break;
                    }

                    case(
    $extension_of_file == ".wav"):
                    {
                    
    $content_type "audio/x-wav";
                    break;
                    }

                    case(
    $extension_of_file == ".wma"):
                    {
                    
    $content_type "audio/wma";
                    break;
                    }

                    case(
    $extension_of_file == ".mpg" or $extension_of_file == ".mpeg" or $extension_of_file == ".mpe"):
                    {
                    
    $content_type "video/mpeg";
                    break;
                    }

                    case(
    $extension_of_file == ".mov"):
                    {
                    
    $content_type "video/quicktime";
                    break;
                    }

                    case(
    $extension_of_file == ".avi"):
                    {
                    
    $content_type "video/x-msvideo";
                    break;
                    }

                    default:
                    {
                    
    $content_type "application/force-download";
                    }
                }

            
    header("Content-Type: " $content_type);
            
    header("Content-Length: " $size_of_file);
            
    header("Content-Disposition: attachment; filename=\"" $name_of_file ."\"");

            
    $copied_size 0;

            
    $handler = @fopen($file,"rb");

                while(!
    feof($handler) and !connection_aborted())
                {
                
    set_time_limit(0);

                
    $part_of_file fread($handler,round(($download_speed 102.4),0));

                echo 
    $part_of_file;

                
    ob_flush();
                
    flush();

                
    $copied_size $copied_size strlen($part_of_file);

                
    usleep(100000);
                }

            
    fclose($handler);

                if(
    $copied_size == $size_of_file)
                {
                
    $download_successfuly TRUE;
                }

                else
                {
                
    $download_successfuly FALSE;
                }

            
    $error FALSE;
            }

            else
            {
            
    $error "Die angeforderte Datei existiert zwar, ist aber nicht zum Download freigegeben!";
            }
        }

        else
        {
        
    $error "Die angeforderte Datei konnte nicht gefunden werden!";
        }

    return array(
    $error,$copied_size,$download_successfuly);
    }

       
    download_data("$location");

        if(
    $error/* <- Gucken, ob ein Fehler aufgetaucht ist, wenn js... */
        
    {
        echo 
    $error/* <- ...diesen Fehler ausgeben... */
        
    }
    ?>
    Thanks!
    MoD
     
  2. swordser2 Banned

    7
    72
    <?
    $_GET['location'] = $location;

    function download_data($file,$download_speed = 256)
    {
    if(file_exists($file)) /* Existiert Datei überhaupt? */
    {
    if(is_readable($file)) /* Kann sie auch gelesen werden? */
    {
    $size_of_file = filesize($file); /* Größe der Datei */

    $extension_of_file = strtolower(substr(strrchr($file,"."),0)); /* Datei-Endung filtern */

    $name_of_file = str_replace(" ","%20",basename($file)); /* Leerzeichen ersetzen */

    header("HTTP/1.1 200 OK");

    header("Expires: 0"); /* KEIN cachen */
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: private",FALSE);
    header("Pragma: public");
    header("Content-Description: File Transfer"); /* Download eben xD */
    header("Content-Transfer-Encoding: binary"); /* Uebertragungs-Art */

    switch(TRUE)
    {
    case($extension_of_file == ".exe"):
    {
    $content_type = "application/octet-stream";
    break;
    }

    case($extension_of_file == ".zip"):
    {
    $content_type = "application/zip";
    break;
    }

    case($extension_of_file == ".rar"):
    {
    $content_type = "application/rar";
    break;
    }

    case($extension_of_file == ".doc"):
    {
    $content_type = "application/msword";
    break;
    }

    case($extension_of_file == ".xls"):
    {
    $content_type = "application/vnd.ms-excel";
    break;
    }

    case($extension_of_file == ".ppt"):
    {
    $content_type = "application/vnd.ms-powerpoint";
    break;
    }

    case($extension_of_file == ".gif"):
    {
    $content_type = "image/gif";
    break;
    }

    case($extension_of_file == ".png"):
    {
    $content_type = "image/png";
    break;
    }

    case($extension_of_file == ".jpg" or $extension_of_file == ".jpeg"):
    {
    $content_type = "image/jpg";
    break;
    }

    case($extension_of_file == ".mp3"):
    {
    $content_type = "audio/mpeg";
    break;
    }

    case($extension_of_file == ".wav"):
    {
    $content_type = "audio/x-wav";
    break;
    }

    case($extension_of_file == ".wma"):
    {
    $content_type = "audio/wma";
    break;
    }

    case($extension_of_file == ".mpg" or $extension_of_file == ".mpeg" or $extension_of_file == ".mpe"):
    {
    $content_type = "video/mpeg";
    break;
    }

    case($extension_of_file == ".mov"):
    {
    $content_type = "video/quicktime";
    break;
    }

    case($extension_of_file == ".avi"):
    {
    $content_type = "video/x-msvideo";
    break;
    }

    default:
    {
    $content_type = "application/force-download";
    }
    }

    header("Content-Type: " . $content_type);
    header("Content-Length: " . $size_of_file);
    header("Content-Disposition: attachment; filename=\"" . $name_of_file ."\"");

    $copied_size = 0;

    $handler = @fopen($file,"rb");

    while(!feof($handler) and !connection_aborted())
    {
    set_time_limit(0);

    $part_of_file = fread($handler,round(($download_speed * 102.4),0));

    echo $part_of_file;

    ob_flush();
    flush();

    $copied_size = $copied_size + strlen($part_of_file);

    usleep(100000);
    }

    fclose($handler);

    if($copied_size == $size_of_file)
    {
    $download_successfuly = TRUE;
    }

    else
    {
    $download_successfuly = FALSE;
    }

    $error = FALSE;
    }

    else
    {
    $error = "Die angeforderte Datei existiert zwar, ist aber nicht zum Download freigegeben!";
    }
    }

    else
    {
    $error = "Die angeforderte Datei konnte nicht gefunden werden!";
    }

    return array($error,$copied_size,$download_successfuly);
    }

    download_data("$location");

    if($error) /* <- Gucken, ob ein Fehler aufgetaucht ist, wenn js... */
    {
    echo $error; /* <- ...diesen Fehler ausgeben... */
    }
    ?>

    <a href="download.php?location=images/test.jpg">link</a>

    <a href="download.php?location=images/test.jpg">link</a>
     
Thread Status:
Not open for further replies.