Exemplo n.º 1
0
/**
 * @usage Download Given File
 * @param $filepath
 * @param $filename
 * @param int $speed
 * @param int $resume_support
 * @param array $extras
 */
function wpdm_download_file($filepath, $filename, $speed = 0, $resume_support = 1, $extras = array())
{
    //dd($_SERVER);
    if (isset($extras['package'])) {
        $package = $extras['package'];
    }
    $mdata = wp_check_filetype($filename);
    $content_type = $mdata['type'];
    $buffer = $speed ? $speed : 1024;
    //$buffer *= 1024; // in byte
    $bandwidth = 0;
    @ini_set('zlib.output_compression', 'Off');
    @set_time_limit(0);
    @session_cache_limiter('none');
    @ob_end_clean();
    @session_write_close();
    //@ob_clean();
    if (strpos($filepath, '://')) {
        $filepath = wpdm_cache_remote_file($filepath, $filename);
    }
    if (file_exists($filepath)) {
        $fsize = filesize($filepath);
    } else {
        $fsize = 0;
    }
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Robots: none");
    header("Content-type: {$content_type}");
    header("Content-disposition: attachment;filename=\"{$filename}\"");
    header("Content-Transfer-Encoding: binary");
    if (isset($_REQUEST['play']) && strpos($_SERVER['HTTP_USER_AGENT'], "Safari")) {
        header("Content-Length: " . $fsize);
        readfile($filepath);
        die;
    }
    $file = fopen($filepath, "rb");
    //check if http_range is sent by browser (or download manager)
    if (isset($_SERVER['HTTP_RANGE']) && $fsize > 0) {
        list($bytes, $http_range) = explode("=", $_SERVER['HTTP_RANGE']);
        $set_pointer = intval(array_shift($tmp = explode('-', $http_range)));
        $new_length = $fsize - $set_pointer;
        header("Accept-Ranges: bytes");
        header("HTTP/1.1 206 Partial Content");
        header("Content-Length: {$new_length}");
        header("Content-Range: bytes {$http_range}{$fsize}/{$fsize}");
        fseek($file, $set_pointer);
    } else {
        header("Content-Length: " . $fsize);
    }
    $packet = 1;
    if ($file) {
        while (!(connection_aborted() || connection_status() == 1) && $fsize > 0) {
            if ($fsize > $buffer) {
                echo fread($file, $buffer);
            } else {
                echo fread($file, $fsize);
            }
            flush();
            $fsize -= $buffer;
            $bandwidth += $buffer;
            if ($speed > 0 && $bandwidth > $speed * $packet * 1024) {
                sleep(1);
                $packet++;
            }
        }
        $package['downloaded_file_size'] = $fsize;
        //add_action('wpdm_download_completed', $package);
        @fclose($file);
    }
}
Exemplo n.º 2
0
/**
 * @usage Download Given File
 * @param $filepath
 * @param $filename
 * @param int $speed
 * @param int $resume_support
 * @param array $extras
 */
function wpdm_download_file($filepath, $filename, $speed = 0, $resume_support = 1, $extras = array())
{
    if (isset($extras['package'])) {
        $package = $extras['package'];
    }
    $mdata = wp_check_filetype($filename);
    $content_type = $mdata['type'];
    $buffer = $speed ? $speed : 1024;
    $buffer *= 1024;
    // in byte
    $bandwidth = 0;
    if (function_exists('ini_set')) {
        @ini_set('display_errors', 0);
    }
    @session_write_close();
    //if ( function_exists( 'apache_setenv' ) )
    //    @apache_setenv( 'no-gzip', 1 );
    if (function_exists('ini_set')) {
        @ini_set('zlib.output_compression', 'Off');
    }
    @set_time_limit(0);
    @session_cache_limiter('none');
    if (get_option('__wpdm_support_output_buffer', 1) == 1) {
        do {
            @ob_end_clean();
        } while (ob_get_level() > 0);
    }
    if (strpos($filepath, '://')) {
        $filepath = wpdm_cache_remote_file($filepath, $filename);
    }
    if (file_exists($filepath)) {
        $fsize = filesize($filepath);
    } else {
        $fsize = 0;
    }
    nocache_headers();
    header("X-Robots-Tag: noindex, nofollow", true);
    header("Robots: none");
    header("Content-type: {$content_type}");
    if (get_option('__wpdm_open_in_browser', 0)) {
        header("Content-disposition: inline;filename=\"{$filename}\"");
    } else {
        header("Content-disposition: attachment;filename=\"{$filename}\"");
    }
    header("Content-Transfer-Encoding: binary");
    if (isset($_REQUEST['play']) && strpos($_SERVER['HTTP_USER_AGENT'], "Safari") || get_option('__wpdm_download_resume', 1) == 2) {
        readfile($filepath);
        die;
    }
    $file = @fopen($filepath, "rb");
    //check if http_range is sent by browser (or download manager)
    if (isset($_SERVER['HTTP_RANGE']) && $fsize > 0) {
        list($bytes, $http_range) = explode("=", $_SERVER['HTTP_RANGE']);
        $set_pointer = intval(array_shift($tmp = explode('-', $http_range)));
        $new_length = $fsize - $set_pointer;
        header("Accept-Ranges: bytes");
        header("HTTP/1.1 206 Partial Content");
        header("Content-Length: {$new_length}");
        header("Content-Range: bytes {$http_range}{$fsize}/{$fsize}");
        fseek($file, $set_pointer);
    } else {
        header("Content-Length: " . $fsize);
    }
    $packet = 1;
    if ($file) {
        while (!(connection_aborted() || connection_status() == 1) && $fsize > 0) {
            if ($fsize > $buffer) {
                echo fread($file, $buffer);
            } else {
                echo fread($file, $fsize);
            }
            ob_flush();
            flush();
            $fsize -= $buffer;
            $bandwidth += $buffer;
            if ($speed > 0 && $bandwidth > $speed * $packet * 1024) {
                sleep(1);
                $packet++;
            }
        }
        $package['downloaded_file_size'] = $fsize;
        //add_action('wpdm_download_completed', $package);
        @fclose($file);
    }
    die;
}