PDA

View Full Version : سوال: اساتيد كمك كنن سيستم دانلود



p30portable
دوشنبه 31 خرداد 1389, 13:04 عصر
سلام به دوستان عزيز من آموزش زير رو ميخوام كسي لينك اموزش يا يه نمونه اسكريپت سراغ داره ؟ ( php )


مدیریت فایل های قابل دانلود، مخفی کردن فایل ها در سرور، پشتیبانی Resume هنگام دانلود فایل ها
یکی از روش های رایج فروش محصولات نرم افزاری فروش مجوز استفاده از محصول و سپس دادن اجازه دانلود به کاربران مجاز است. برای دستیابی به این هدف سیستم نرم افزاری سایت میبایست قابلیت Streaming Download را دارا باشد تا صاحب سایت بتواند بدون ارائه کردن لینک مستقیم فایل بر روی سرور دانلود آن را ممکن کند. همچنین قابلیت دریافت ادامه فایل در مراجعه بعدی کاربر به سایت (معروف به Download Resume) از ضروریات چنین سیستمی است.

pashaie
دوشنبه 31 خرداد 1389, 14:06 عصر
خدمت شما:


<?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;
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_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;
}

?>

alih110
دوشنبه 31 خرداد 1389, 15:11 عصر
ممنون منم به یه همچین چیز نیاز داشتم حالا میشه در مورد نحوه استفادش یه توضیح بدید

p30portable
دوشنبه 31 خرداد 1389, 15:19 عصر
فكر كنم مشكل داره
function
dl_file_resumable
صدا زدم و به متغير $file آدرس يه فايل رو دادم اما به جاي دانلود فايل انگار فايل zip رو با notpad باز كني صفحه رو نشون داد

تو لوكال تست كردم !

مشكل كجاست ؟

p30portable
دوشنبه 31 خرداد 1389, 15:27 عصر
روي هاست هم تست كردم مشكل داره !

pashaie
دوشنبه 31 خرداد 1389, 16:45 عصر
ممنون منم به یه همچین چیز نیاز داشتم حالا میشه در مورد نحوه استفادش یه توضیح بدید
نحوه استفاده از اش که خیلی سادس، این تابع رو تو یه فایلی قرار می دی، مثلا dl.php بعد تو فایل اصلیت فایل dl.php رو include می کنی و در آخر تابع dl_file_resumable(); رو با مسیر فایل مورد نظر فراخونی می کنی.
مثلا اگه فایل zar.rar با باقی فایلها تو یه شاخه باشه، کد


dl_file_resumable("zar.rar");
باعث شروع دانلود می شه.


فكر كنم مشكل داره
function
dl_file_resumable
صدا زدم و به متغير $file آدرس يه فايل رو دادم اما به جاي دانلود فايل انگار فايل zip رو با notpad باز كني صفحه رو نشون داد
والا من خودم الان دوباره تست کردم مشکلی نداشت.
با لوکال هسات تست کردم.
php 5.2.8
apache 2.2.11

اگه باز مشکلت رفع نشد، کد زیر رو هم امتحان کن


<?

/**
@author Nguyen Quoc Bao <quocbao.coder@gmail.com>
@version 1.3
@desc A simple object for processing download operation , support section downloading
Please send me an email if you find some bug or it doesn't work with download manager.
I've tested it with
- Reget
- FDM
- FlashGet
- GetRight
- DAP

@copyright It's free as long as you keep this header .
@example

1: File Download
$object = new downloader;
$object->set_byfile($filename); //Download from a file
$object->use_resume = true; //Enable Resume Mode
$object->download(); //Download File

2: Data Download
$object = new downloader;
$object->set_bydata($data); //Download from php data
$object->use_resume = true; //Enable Resume Mode
$object->set_filename($filename); //Set download name
$object->set_mime($mime); //File MIME (Default: application/otect-stream)
$object->download(); //Download File

3: Manual Download
$object = new downloader;
$object->set_filename($filename);
$object->download_ex($size);
//output your data here , remember to use $this->seek_start and $this->seek_end value :)

**/

class httpdownload {

var $data = null;
var $data_len = 0;
var $data_mod = 0;
var $data_type = 0;
var $data_section = 0; //section download
/**
* @var ObjectHandler
**/
var $handler = array('auth' => null);
var $use_resume = true;
var $use_autoexit = false;
var $use_auth = false;
var $filename = null;
var $mime = null;
var $bufsize = 2048;
var $seek_start = 0;
var $seek_end = -1;

/**
* Total bandwidth has been used for this download
* @var int
*/
var $bandwidth = 0;
/**
* Speed limit
* @var float
*/
var $speed = 0;

/*-------------------
| Download Function |
-------------------*/
/**
* Check authentication and get seek position
* @return bool
**/
function initialize() {
global $HTTP_SERVER_VARS;

if ($this->use_auth) //use authentication
{
if (!$this->_auth()) //no authentication
{
header('WWW-Authenticate: Basic realm="Please enter your username and password"');
header('HTTP/1.0 401 Unauthorized');
header('status: 401 Unauthorized');
if ($this->use_autoexit) exit();
return false;
}
}
if ($this->mime == null) $this->mime = "application/octet-stream"; //default mime

if (isset($_SERVER['HTTP_RANGE']) || isset($HTTP_SERVER_VARS['HTTP_RANGE']))
{

if (isset($HTTP_SERVER_VARS['HTTP_RANGE'])) $seek_range = substr($HTTP_SERVER_VARS['HTTP_RANGE'] , strlen('bytes='));
else $seek_range = substr($_SERVER['HTTP_RANGE'] , strlen('bytes='));

$range = explode('-',$seek_range);

if ($range[0] > 0)
{
$this->seek_start = intval($range[0]);
}

if ($range[1] > 0) $this->seek_end = intval($range[1]);
else $this->seek_end = -1;

if (!$this->use_resume)
{
$this->seek_start = 0;

//header("HTTP/1.0 404 Bad Request");
//header("Status: 400 Bad Request");

//exit;

//return false;
}
else
{
$this->data_section = 1;
}

}
else
{
$this->seek_start = 0;
$this->seek_end = -1;
}

return true;
}
/**
* Send download information header
**/
function header($size,$seek_start=null,$seek_end=null) {
header('Content-type: ' . $this->mime);
header('Content-Disposition: attachment; filename="' . $this->filename . '"');
header('Last-Modified: ' . date('D, d M Y H:i:s \G\M\T' , $this->data_mod));

if ($this->data_section && $this->use_resume)
{
header("HTTP/1.0 206 Partial Content");
header("Status: 206 Partial Content");
header('Accept-Ranges: bytes');
header("Content-Range: bytes $seek_start-$seek_end/$size");
header("Content-Length: " . ($seek_end - $seek_start + 1));
}
else
{
header("Content-Length: $size");
}
}

function download_ex($size)
{
if (!$this->initialize()) return false;
ignore_user_abort(true);
//Use seek end here
if ($this->seek_start > ($size - 1)) $this->seek_start = 0;
if ($this->seek_end <= 0) $this->seek_end = $size - 1;
$this->header($size,$seek,$this->seek_end);
$this->data_mod = time();
return true;
}

/**
* Start download
* @return bool
**/
function download() {
if (!$this->initialize()) return false;

$seek = $this->seek_start;
$speed = $this->speed;
$bufsize = $this->bufsize;
$packet = 1;

//do some clean up
@ob_end_clean();
$old_status = ignore_user_abort(true);
@set_time_limit(0);
$this->bandwidth = 0;

$size = $this->data_len;

if ($this->data_type == 0) //download from a file
{

$size = filesize($this->data);
if ($seek > ($size - 1)) $seek = 0;
if ($this->filename == null) $this->filename = basename($this->data);

$res = fopen($this->data,'rb');
if ($seek) fseek($res , $seek);
if ($this->seek_end < $seek) $this->seek_end = $size - 1;

$this->header($size,$seek,$this->seek_end); //always use the last seek
$size = $this->seek_end - $seek + 1;

while (!(connection_aborted() || connection_status() == 1) && $size > 0)
{
if ($size < $bufsize)
{
echo fread($res , $size);
$this->bandwidth += $size;
}
else
{
echo fread($res , $bufsize);
$this->bandwidth += $bufsize;
}

$size -= $bufsize;
flush();

if ($speed > 0 && ($this->bandwidth > $speed*$packet*1024))
{
sleep(1);
$packet++;
}
}
fclose($res);

}

elseif ($this->data_type == 1) //download from a string
{
if ($seek > ($size - 1)) $seek = 0;
if ($this->seek_end < $seek) $this->seek_end = $this->data_len - 1;
$this->data = substr($this->data , $seek , $this->seek_end - $seek + 1);
if ($this->filename == null) $this->filename = time();
$size = strlen($this->data);
$this->header($this->data_len,$seek,$this->seek_end);
while (!connection_aborted() && $size > 0) {
if ($size < $bufsize)
{
$this->bandwidth += $size;
}
else
{
$this->bandwidth += $bufsize;
}

echo substr($this->data , 0 , $bufsize);
$this->data = substr($this->data , $bufsize);

$size -= $bufsize;
flush();

if ($speed > 0 && ($this->bandwidth > $speed*$packet*1024))
{
sleep(1);
$packet++;
}
}
} else if ($this->data_type == 2) {
//just send a redirect header
header('location: ' . $this->data);
}

if ($this->use_autoexit) exit();

//restore old status
ignore_user_abort($old_status);
set_time_limit(ini_get("max_execution_time"));

return true;
}

function set_byfile($dir) {
if (is_readable($dir) && is_file($dir)) {
$this->data_len = 0;
$this->data = $dir;
$this->data_type = 0;
$this->data_mod = filemtime($dir);
return true;
} else return false;
}

function set_bydata($data) {
if ($data == '') return false;
$this->data = $data;
$this->data_len = strlen($data);
$this->data_type = 1;
$this->data_mod = time();
return true;
}

function set_byurl($data) {
$this->data = $data;
$this->data_len = 0;
$this->data_type = 2;
return true;
}

function set_lastmodtime($time) {
$time = intval($time);
if ($time <= 0) $time = time();
$this->data_mod = $time;
}

/**
* Check authentication
* @return bool
**/
function _auth() {
if (!isset($_SERVER['PHP_AUTH_USER'])) return false;
if (isset($this->handler['auth']) && function_exists($this->handler['auth']))
{
return $this->handler['auth']('auth' , $_SERVER['PHP_AUTH_USER'],$_SERVER['PHP_AUTH_PW']);
}
else return true; //you must use a handler
}

}

?>

مثال برای کد بالا:


<?

$FILENAME = ""; //SET YOUR FILE HERE

if (!$FILENAME || !file_exists($FILENAME)) {
echo "Please set your target file \$FILENAME on line 2\n";
exit();
}

include_once "class.httpdownload.php";

$object = new httpdownload;

$bandwidth = @intval(implode('',file('bandwidth.txt'))) / 1024;
if ($bandwidth > 1024)
{
$bandwidth = round($bandwidth / 1024 , 2);
$bandwidth .= " MB";
}
else
{
$bandwidth .= " KB";
}

switch (@$_GET['download']) {
case 'resume_speed':
case 'noresume_speed':
case 'resume':
case 'noresume':
$object->set_byfile($FILENAME);
if ($_GET['download'] == 'noresume' || $_GET['download'] == 'noresume_speed') $object->use_resume = false;
if ($_GET['download'] == 'resume_speed' || $_GET['download'] == 'noresume_speed' ) $object->speed = 100;
$object->download();
break;
case 'data':
case 'dataresume':
$data = implode('' , file($FILENAME));
$object->set_bydata($data);
if ($_SERVER['download'] != 'dataresume') $object->use_resume = false;
$object->filename = basename($FILENAME);
$object->download();
break;
case 'auth':
$object->set_byfile($FILENAME);
$object->use_auth = true;
$object->handler['auth'] = "test_auth";
$object->download();
break;
case 'url':
$object->set_byurl('http://www.php.net/get/php_manual_chm.zip/from/cr.php.net/mirror');
$object->download();
break;
}

if ($object->bandwidth > 0)
{
error_reporting(E_NONE);
$b = intval(implode('',file('bandwidth.txt'))) + $object->bandwidth;
$f = fopen('bandwidth.txt','wb');
fwrite($f,$b);
fclose($f);
exit;
}

function test_auth($user,$pass) { //test authentication function
if ($user == 'user' && $pass == 'pass') return true;
return false;
}

?>

<head>
<style>
<!--
body { font-family: Tahoma; font-size: 12px }
a { color: #FF0000 }
-->
</style>
</head>

<title>HTTPDownload example</title>

<h2><font color="navy">HttpDownload</font></h2>Select a link and try it with a download manager (like <a href="http://reget.com">Reget</a>) .<br><br>

Total bandwidth used : <B><?=$bandwidth?></B>

<br><br>
<a href="test.php?download=noresume">Download file</a><br>
<a href="test.php?download=noresume_speed">Download file (speed limit 100 kbs)</a><br>
<a href="test.php?download=resume">Download file with resume</a><br>
<a href="test.php?download=resume_speed">Download file with resume (speed limit 100 kbs) </a><br>
<a href="test.php?download=data">Download file data (May slow)</a><br>
<a href="test.php?download=dataresume">Download file data with resume (May slow)</a><br>
<a href="test.php?download=auth">Authentication download (user/pass)</a><br>
<a href="test.php?download=url">URL Download (simple redirect)</a><br>

<p><font size="1"><font color="#808080">( Click
<a href="http://en.vietapi.com/wiki/index.php/PHP:_HttpDownload">
<font color="black">here</font></a><font color=""> to view class
information )</font></p>


منبع کلد بالایی
http://www.phpclasses.org/package/2221-PHP-Server-files-for-downloading-with-resume-support.html

p30portable
دوشنبه 31 خرداد 1389, 17:25 عصر
اين پيغام خطا رو برام مشون ميده



Notice: Undefined variable: path_info in c:\program files\easyphp1-8\www\dl.php on line 21

Notice: Undefined offset: 1 in c:\program files\easyphp1-8\www\dl.php on line 56

Warning: Cannot modify header information - headers already sent by (output started at c:\program files\easyphp1-8\www\dl.php:103) in c:\program files\easyphp1-8\www\dl.php on line 72

Warning: Cannot modify header information - headers already sent by (output started at c:\program files\easyphp1-8\www\dl.php:103) in c:\program files\easyphp1-8\www\dl.php on line 73

Warning: Cannot modify header information - headers already sent by (output started at c:\program files\easyphp1-8\www\dl.php:103) in c:\program files\easyphp1-8\www\dl.php on line 80

Warning: Cannot modify header information - headers already sent by (output started at c:\program files\easyphp1-8\www\dl.php:103) in c:\program files\easyphp1-8\www\dl.php on line 81

Warning: Cannot modify header information - headers already sent by (output started at c:\program files\easyphp1-8\www\dl.php:103) in c:\program files\easyphp1-8\www\dl.php on line 82
PK
Notice: ob_flush(): failed to flush buffer. No buffer to flush. in c:\program files\easyphp1-8\www\dl.php on line 96

p30portable
دوشنبه 31 خرداد 1389, 17:32 عصر
ولي با كد دومي مشكلم حل شد دستت درد نكنه باز شما به فكر من بوديد مرسي

حالا يه سوال چي كار كنم كه ليك دانلود كليك كرد اجرا نشه فقط از روي سايت كليك كنه اجرا بشه

متوجه منظورم شديد ؟

pashaie
دوشنبه 31 خرداد 1389, 17:51 عصر
اين پيغام خطا رو برام مشون ميده
اره مثل اینکه مورد داره!
اول فایل کد زیر رو بنویسی مشکلت حل می شه


error_reporting(0);


حالا يه سوال چي كار كنم كه ليك دانلود كليك كرد اجرا نشه فقط از روي سايت كليك كنه اجرا بشه
متوجه منظورم شديد ؟

فکر کنم منظورتو متوجه شدم. شما می خوای لینک اصلی رو کسی نداشته باشه و بعد از کلیک کردن رو یه اسکریپتی؛ بعد از چک کردن مثلا وضعیت لاگین بودن و ... دانلود فایل شروع شه.

خب یه راه خیلی ساده اینه که اصلا لینک دانلود رو و به کاربرا نشون ندی و فایلات رو هم تو یه شاخه با اسم رندم آپ کنی، تو جایی هم که می خوای لینک دانلود رو بزاری همچین چیزی بنویسی

<a href="mydl.php?f=test.zip">
بعد تو فایل mydl.php شرایط مورد نظر رو چک کنی اگه همچی درست بود، فایل رو باتوجه به شاخه ای که توش آپلود کردی، برای کاربر بفرستی.

p30portable
دوشنبه 31 خرداد 1389, 17:58 عصر
خوب دوست عزيز

كد زير رو بزارم كه ديگه سايت پيغام خطا نميده اما كار هم نميكنه ديگه درسته ؟

error_reporting(0);

نه منظورم لينك اصلي نيست !
ببين من ميخوام به كاربران vip كه حق عضويت ميدن فقط بتونن دانلود كنن .
بعد نميخوام وقتي كه كابري جز VIP ميشه بتونه لينك دانلود رو جاي ديگه بزاره

متوجه شدي دوست عزيز ؟

rapidpich
سه شنبه 01 تیر 1389, 11:33 صبح
برا اینکار نیاز به یه سیستم داری که مدیریت کاربر کنه.
سشن و اینا تو فروم هست اگه جستجو کنی

p30portable
سه شنبه 01 تیر 1389, 12:58 عصر
برا اینکار نیاز به یه سیستم داری که مدیریت کاربر کنه.
سشن و اینا تو فروم هست اگه جستجو کنی

اخه دوست عزيز اين سوال من چه ربطي به session داره اخه
وقتي سوالي نميدانيد لطفاً جواب نديد و ديگران رو به اشتباه نندازيد

rapidpich
چهارشنبه 02 تیر 1389, 13:49 عصر
سپس دادن اجازه دانلود به کاربران مجاز است.
شما سوال خودتون رو درست بخونید بعد نظر بدید!

p30portable
جمعه 04 تیر 1389, 17:26 عصر
اگه سوال رو از اول بخوني متوجه ميشي !
من نميخوام با شما كل كل كنم عزيزم
اصلاً ممنون كه پست دادي حالا كسي هست بتونه جواب منو بده ؟

rapidpich
شنبه 05 تیر 1389, 09:57 صبح
آره، کسی هست کمک کنه؟ فقط یادتون باشه اگه حال نکنم با جوابتون ممکنه چرت و پرت بگم بتون.
چون نمیدونم اینجا یه فرومه و اونو با کلاس درس اشتباه گرفتم، پس حتما قبل از اینکه صحبت کنید اجازه بگیرید.

funpatogh
یک شنبه 06 تیر 1389, 21:32 عصر
اخه دوست عزيز اين سوال من چه ربطي به session داره اخه
وقتي سوالي نميدانيد لطفاً جواب نديد و ديگران رو به اشتباه نندازيد
دوست عزیز برای اینکه فقط کاربران سیستم شما بتوانند و مجاز باشند که دانلود کنند باید از سشن استفاده کنید تا تشخیص بدهید که فرد عضو هست یا خیر و جواب ایشون درست هست
نمیدونم شما چرا ناراحت شدید

intermanch
دوشنبه 07 تیر 1389, 23:06 عصر
pashaie (http://www.barnamenevis.org/forum/member.php?u=69316) عزیز سلام

ممکنه به سوال من هم یه سر بزنی و جواب کارشناسانه بدی ،خواهش میکنم:
http://www.barnamenevis.org/forum/showthread.php?p=1026898#post1026898 (http://www.barnamenevis.org/forum/showthread.php?p=1026898#post1026898)

صدا زند تابع php با جاوا اسکریپت ضمن فرستادن متغیر

قضیه اجرا شدن یا نشدن دستورات سمت سرور هست خواهش میکنم

intermanch
دوشنبه 07 تیر 1389, 23:13 عصر
میشه به این قسمت یه سر بزنید :::
http://www.barnamenevis.org/forum/showthread.php?p=1026898#post1026898
صدا زند تابع php با جاوا اسکریپت ضمن فرستادن متغیر