コード例 #1
0
 function index()
 {
     // clean file
     $file = str_replace(array("\\", "/"), DIRECTORY_SEPARATOR, urldecode($_GET['file']));
     // print_r($_GET);
     // type
     $type = strip_tags($_GET['type']);
     // get type
     switch ($type) {
         case 'download':
             // buffer
             @ob_end_clean();
             // download
             mgm_force_download($file);
             // flush
             @ob_end_flush();
             exit;
             break;
         case 'upload':
             echo 'upload';
             exit;
             break;
     }
 }
コード例 #2
0
/**
 * Magic Members stream download - with restart functionlity
 *
 * @package MagicMembers
 * @since 2.5
 * @desc force download with restart functionlity, modified for FDM support, issue #876
 * @param string filepath
 * @return none
 */
function mgm_stream_download($filepath)
{
    global $mgm_mimes;
    // check connection
    if (connection_status() != 0) {
        return false;
    }
    // see if the file exists
    if (!is_file($filepath)) {
        die("<b>404 File not found!</b>");
    }
    // size
    $size = @filesize($filepath);
    $fileinfo = @pathinfo($filepath);
    // error
    if ($size == 0) {
        die('Empty file! download aborted');
    }
    // resume flag
    $is_resume = true;
    $range = '';
    // check if http_range is sent by browser (or download manager)
    if ($is_resume && isset($_SERVER['HTTP_RANGE'])) {
        // split
        list($size_unit, $range_orig) = explode('=', $_SERVER['HTTP_RANGE'], 2);
        // check
        if ($size_unit == 'bytes') {
            // multiple ranges could be specified at the same time, but for simplicity only serve the first range
            // http://tools.ietf.org/id/draft-ietf-http-range-retrieval-00.txt
            list($range, $extra_ranges) = explode(',', $range_orig, 2);
        }
    }
    // backward compatibility, when ranges empty, iss #1772
    if (empty($range)) {
        mgm_force_download($filepath);
        exit;
    }
    // ----- calculation for ranges
    // 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
    // set filename
    $filename = strstr(mgm_server_var('HTTP_USER_AGENT'), "MSIE") ? preg_replace('/\\./', '%2e', $fileinfo['basename'], substr_count($fileinfo['basename'], '.') - 1) : $fileinfo['basename'];
    // extension
    $file_extension = strtolower($fileinfo['extension']);
    // default mime if we can't find it
    if (isset($mgm_mimes[$file_extension])) {
        // from mimes
        $content_type = is_array($mgm_mimes[$file_extension]) ? $mgm_mimes[$file_extension][0] : $mgm_mimes[$file_extension];
    } else {
        // default
        $content_type = 'application/force-download';
        //'application/octet-stream';
    }
    //figure out download piece from range (if set)
    if (strpos($range, '-') !== false) {
        list($seek_start, $seek_end) = explode('-', $range, 2);
    } else {
        $seek_start = 0;
        $seek_end = '';
    }
    // set start and end based on range (if set), else set defaults
    // also check for invalid ranges.
    $seek_end = empty($seek_end) ? $size - 1 : min(abs(intval($seek_end)), $size - 1);
    $seek_start = empty($seek_start) || $seek_end < abs(intval($seek_start)) ? 0 : max(abs(intval($seek_start)), 0);
    //issue #1375
    @session_write_close();
    // kill quotes
    if (function_exists('set_magic_quotes_runtime')) {
        @set_magic_quotes_runtime(0);
    }
    //add headers if resumable
    if ($is_resume) {
        // Only send partial content header if downloading a piece of the file (IE workaround)
        if ($seek_start > 0 || $seek_end < $size - 1) {
            // header
            @header('HTTP/1.1 206 Partial Content');
            // log
            // mgm_log("Partial Content {$seek_start}-{$seek_end}/{$size}", __FUNCTION__ . '_size_range');
        }
        // others
        @header('Accept-Ranges: bytes');
        @header('Content-Range: bytes ' . $seek_start . '-' . $seek_end . '/' . $size);
    }
    //headers for IE Bugs (is this necessary?)
    //header("Cache-Control: cache, must-revalidate");
    //header("Pragma: public");
    @header('Content-Type: ' . $content_type);
    @header('Content-Disposition: attachment; filename="' . $filename . '"');
    @header('Content-Length: ' . ($seek_end - $seek_start + 1));
    //open the file
    $fp = @fopen($filepath, 'rb');
    // seek to start of missing part
    @fseek($fp, $seek_start);
    //mgm_log(memory_get_peak_usage(true)."before download", __FUNCTION__);
    //start buffered download
    while (!feof($fp)) {
        //reset time limit for big files
        @set_time_limit(0);
        @ini_set('memory_limit', 1073741824);
        // 1024M
        print @fread($fp, 1024 * 8);
        @ob_flush();
        @flush();
        // sleep
        @usleep(1000);
        // flush
        @ob_end_flush();
        //keep this as there were some memory related issues(#545)
    }
    // close
    @fclose($fp);
    //mgm_log(memory_get_peak_usage(true)."after download", __FUNCTION__);
    exit;
    // end
    return connection_status() == 0 && !connection_aborted();
}