نمایش نتایج: از شماره 1 تا 8 , از مجموع 8

موضوع: اجازه دانلود به کاربران vip در سرور دانلود (بدون mysql)

Threaded View

پست قبلی پست قبلی   پست بعدی پست بعدی
  1. #1
    عضو جدید
    تاریخ عضویت
    Nov 2011
    محل سکونت
    C:\xampp\htdocs
    نوشته ها
    29
    تشکر تشکر کرده 
    69
    تشکر تشکر شده 
    13
    تشکر شده در
    7 پست

    Question اجازه دانلود به کاربران vip در سرور دانلود (بدون mysql)

    با سلام
    خسته نباشین


    دوستان یک سایت فروش فایل داریم که روی سرور یک قرار داره. این سایت فایل های خودش رو در سرور 2 که مخصوص دانلوده و بدون mysql هست قرار میده. حالا با کمک php و htaccess کاری کردم که هر کاربر برای دانلود یک فایل خاص (مثلا abc.mp4)، یک لینک یکتا داشته باشه و نباید بتونه با آدرس اصلی فایل، اون رو دانلود کنه. (مثال زیر)


    کاربر یک: http://example.com/files/abc.mp4 <----- http://example.com/er3457h.mp4
    کاربر دو: http://example.com/files/abc.mp4 <----- http://example.com/3Rg4tre.mp4
    کاربر سه: http://example.com/files/abc.mp4 <----- http://example.com/zf4tYjK.mp4


    مشکلی که الان درگیرش هستم اینه که وقتی کاربر در سرور 2 می خواد فایل رو دانلود کنه، اگر برای انجام دانلود از تابع readfile استفاده کنم، بدلیل اینکه حجم فایل ها بیشتر از 500 مگابایت هست، اگر تعداد زیادی به سرور متصل بشن، دچار کمبود رم خواهیم شد و سرور دان میشه. روش های زیادی هم مثل بافر و ... تست کردم فرقی نکرد. متاسفانه روش x-sendfile بدلیل اینکه امکان فعالسازیش روی سرور 2 نیست قابل اجرا نیست.


    دوستان کسی میتونه راهنمایی کنه؟
    راهی نداره که بشه با php و htaccess به روش http فایل رو دانلود کنیم تا رم مصرف نشه؟

    متاسفانه روش های زیر مناسب نبودن




    نمونه کد (1) برای دانلود در سرور دو:
    کد PHP:
    header("Content-Type: video/mp4");readfile("./files/abc.mp4"); 



    نمونه کد (2) برای دانلود در سرور دو:
    کد PHP:
    function resumableDownload($file,$filename,$file_size,$content_type,$disposition 'inline'){    @set_time_limit(0);    session_write_close();    // turn off compression on the server    @ini_set('zlib.output_compression', 'Off');        $time = filemtime($file);    $etag = md5($file.$time);        $fileopen = fopen($file, "rb");    // Download speed in KB/s    $chunk = 8*1024;    // Initialize the range of bytes to be transferred    $start = 0;    $end = $file_size-1;    // Check HTTP_RANGE variable    if (        isset($_SERVER)         && preg_match('/^bytes=(\d+)-(\d*)/', $_SERVER, $arr)    )    {        $start = $arr;        if ($arr) {            $end = $arr;        }    }    // Check if starting and ending byte is valid    if ($start > $end || $start > $file_size-1) {    header("HTTP/1.1 416 Requested Range Not Satisfiable");    header("Content-Length: 0");    return false;    } else {    // For the first time download    if ($start == 0 && $end == $file_size) {    // Send HTTP OK header    header("HTTP/1.1 200 OK");    } else {    // For resume download    header("HTTP/1.1 206 Partial Content");    header("Content-Range: bytes ".$start."-".$end."/".$file_size);    }    // Bytes left    $left = $end-$start+1;        //set last-modified header    header('Last-Modified: '.date('D, d M Y H:i:s \G\M\T', $time));    //set etag-header    header('ETag: "'.$etag.'"');        // Send the other headers    header("Accept-Ranges: bytes");    header("Content-Length: ".$left);    header("Content-Type: $content_type");    header("Content-Disposition: $disposition; filename=\"".$filename."\"");    header("Content-Transfer-Encoding: binary");    header("Expires: -1");    // Read file from the given starting bytes    fseek($fileopen, $start);        while (!feof($fileopen)) {    echo fread($fileopen, $chunk);    @ob_flush();    flush();    $left-=$chunk;    // // Delay for 10 microsecond    // usleep(10);    }    }    fclose($fileopen);    return true;}
    resumableDownload("./files/abc.mp4" "abc.mp4" sizeof("./files/abc.mp4") , "video/mp4" 'inline'); 

    نمونه کد (3) برای دانلود در سرور دو:
    کد PHP:
    function download_file($file,$ctype$is_resume=TRUE){    //First, see if the file exists    if (!is_file($file))    {        die("<b>404 File not found!</b>");    }    //Gather relevent info about file    $size = filesize($file);    $fileinfo = pathinfo($file);    //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    $filename = (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE')) ?        preg_replace('/\./', '%2e', $fileinfo['basename'], substr_count($fileinfo['basename'], '.') - 1) :        $fileinfo['basename'];    //check if http_range is sent by browser (or download manager)    if($is_resume && isset($_SERVER['HTTP_RANGE']))    {        list($size_unit, $range_orig) = explode('=', $_SERVER['HTTP_RANGE'], 2);        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);        }        else        {            $range = '';        }    }    else    {        $range = '';    }    //figure out download piece from range (if set)    list($seek_start, $seek_end) = explode('-', $range, 2);    //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);    //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('HTTP/1.1 206 Partial Content');        }        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: ' . $ctype);    header('Content-Disposition: attachment; filename="' . $filename . '"');    header('Content-Length: '.($seek_end - $seek_start + 1));    //open the file    $fp = fopen($file, 'rb');    //seek to start of missing part    fseek($fp, $seek_start);    //start buffered download    while(!feof($fp))    {        //reset time limit for big files        set_time_limit(0);        print(fread($fp, 1024*8));        flush();        ob_flush();    }    fclose($fp);    exit;}
    download_file("./files/abc.mp4""video/mp4" TRUE); 



    نمونه کد (4) برای دانلود در سرور دو:
    کد PHP:
    function dl_file_resumable($file$is_resume=TRUE){
        
    //First, see if the file exists
        
    if (!is_file($file))
        {
            die(
    "<b>404 File not found!</b>");
        }


        
    //Gather relevent info about file
        
    $size filesize($file);
        
    $fileinfo pathinfo($file);


        
    //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
        
    $filename = (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE')) ?
                    
    preg_replace('/\./''%2e'$fileinfo['basename'], substr_count($fileinfo['basename'], '.') - 1) :
                    
    $fileinfo['basename'];


        
    $file_extension strtolower($path_info['extension']);


        
    //This will set the Content-Type to the appropriate setting for the file
        
    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;
            case 
    'mp4'$ctype='video/mp4'; break;
            default:    
    $ctype='application/force-download';
        }


        
    //check if http_range is sent by browser (or download manager)
        
    if($is_resume && isset($_SERVER['HTTP_RANGE']))
        {
            list(
    $size_unit$range_orig) = explode('='$_SERVER['HTTP_RANGE'], 2);


            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_orig2);
            }
            else
            {
                
    $range '';
            }
        }
        else
        {
            
    $range '';
        }


        
    //figure out download piece from range (if set)
        
    list($seek_start$seek_end) = explode('-'$range2);


        
    //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))) ? max(abs(intval($seek_start)),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 || $seek_end < ($size 1))
            {
                
    header('HTTP/1.1 206 Partial Content');
            }


            
    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: ' $ctype);
        
    header('Content-Disposition: attachment; filename="' $filename '"');
        
    header('Content-Length: '.($seek_end $seek_start 1));


        
    //open the file
        
    $fp fopen($file'rb');
        
    //seek to start of missing part
        
    fseek($fp$seek_start);


        
    //start buffered download
        
    while(!feof($fp))
        {
            
    //reset time limit for big files
            
    set_time_limit(0);
            print(
    fread($fp1024*8));
            
    flush();
            
    ob_flush();
        }


        
    fclose($fp);
        exit;
    }
    dl_file_resumable("./files/abc.mp4"TRUE); 
    ویرایش توسط mghhgm : August 9th, 2017 در ساعت 10:12

اطلاعات موضوع

کاربرانی که در حال مشاهده این موضوع هستند

در حال حاضر 1 کاربر در حال مشاهده این موضوع است. (0 کاربران و 1 مهمان ها)

موضوعات مشابه

  1. /var/log/mysql/mysql-slow.log در بک اپ گیری
    توسط mha1368 در انجمن سوالات و مشکلات
    پاسخ ها: 3
    آخرين نوشته: October 11th, 2015, 23:36
  2. پاک شدن mysql.sock و استارت نشدن mysql
    توسط Hooramin در انجمن سوالات و مشکلات
    پاسخ ها: 4
    آخرين نوشته: August 24th, 2013, 19:48
  3. پاسخ ها: 14
    آخرين نوشته: August 11th, 2013, 00:44
  4. ارور Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (11)
    توسط pc.rootall در انجمن سوالات و مشکلات
    پاسخ ها: 8
    آخرين نوشته: July 20th, 2013, 16:37

مجوز های ارسال و ویرایش

  • شما نمیتوانید موضوع جدیدی ارسال کنید
  • شما امکان ارسال پاسخ را ندارید
  • شما نمیتوانید فایل پیوست کنید.
  • شما نمیتوانید پست های خود را ویرایش کنید
  •